2014-04-29 177 views
-1

目前我正在製作一個允許用戶從谷歌地圖獲取座標的程序。該程序使用鼠標指針獲取地點的位置。如果沒有預定的地方,它應該得到一個正確的座標,但是如果有什麼東西(火車,餐館,醫院)我不能得到座標。我如何獲得預定義位置的座標?如何在谷歌地圖預定義位置添加標記

謝謝...

回答

1

我認爲以下是你想要的。

<!DOCTYPE html> 
<html> 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <title>Simple markers</title> 
    <style> 
     html, body, #map-canvas { 
     height: 100%; 
     margin: 0px; 
     padding: 0px 
     } 
    </style> 
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> 
<script> 
    var map,marker; 
    var infowindow = new google.maps.InfoWindow(); 
    var geocoder = new google.maps.Geocoder(); 
function initialize() { 
    var myLatlng = new google.maps.LatLng(-25.363882,131.044922); 
    var mapOptions = { 
    zoom: 4, 
    center: myLatlng, 
    mapTypeId: 'roadmap' 
    } 
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
    google.maps.event.addListener(map,'click',function(event) { 
    geocode(event.latLng); 
    }); 
} 
function geocode(position) { 
     geocoder.geocode({'latLng': position}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      if (results[1]) { 
       map.setZoom(11); 
       marker = new google.maps.Marker({ 
        position: position, 
        map: map 
       }); 
       infowindow.setContent(results[1].formatted_address); 
       infowindow.open(map, marker); 
      } else { 
      alert('No results found'); 
     } 
    } else { 
     alert('Geocoder failed due to: ' + status); 
    } 
    });  
} 

google.maps.event.addDomListener(window, 'load', initialize); 
    </script> 
    </head> 
    <body> 
    <div id="map-canvas"></div> 
    </body> 
</html> 

Working Demo。你有沒有發現任何困難隨意提問。

+0

謝謝你的迴應。我試過你的代碼,然後我將縮放級別改爲14.我找到了「烏魯魯 - 卡塔曲塔國家公園」,但是當我點擊樹形圖標時,它不會添加標記。我如何在該地點添加標記? – RicoWijaya