2011-08-16 70 views
3

我有一個問題,設置標記並獲得該標記的經度和緯度。我怎樣才能做到谷歌地圖v3 API谷歌地圖API v3集標記和獲取點

var myLatlng = new google.maps.LatLng(-34.397, 150.644); 
     var myOptions = { 
      zoom: 8, 
      center: myLatlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
var map = new google.maps.Map(document.getElementById("divGoogleMaps"), myOptions); 

google.maps.event.addListener(map, 'click', function() { 

}); 

這是我的開始代碼。

回答

3

你應該檢查谷歌地圖API文檔的網站,他們有一個fex的例子來幫助你開始。

http://code.google.com/apis/maps/documentation/javascript/basics.html

google.maps.event.addListener(map, 'click', function(e) { 
      placeMarker(e.latLng, map); 
     }); 
     } 

     function placeMarker(position, map) { 
     var marker = new google.maps.Marker({ 
      position: position, 
      map: map 
     }); 
     map.panTo(position); 
     } 

在這裏你設置一個標記,並得到位置。

+0

最後這段代碼幫我做了這件事。我可以在地圖上只能有一個標記,這個標記,當用戶cliks在地圖上的「代碼」 google.maps.event.addListener(地圖「點擊」設置功能(事件){ \t \t \t如果(self.marker! = NULL) \t \t \t \t self.marker.setMap(空); \t \t \t self.value =新google.maps.LatLng(event.latLng.lat(),event.latLng.lng()); \t \t \t \t self.marker =新google.maps.Marker({ \t \t \t \t位置:0 self.value\t \t \t}); \t \t \t self.zoom = map.getZoom(); \t \t \t self.marker.setMap(map); \t}); – user821738

0
var marker = new google.maps.Marker({ 
    position: myLatlng, 
    map: map, 
    title:"Hello World!" 
}); 

Google Maps API v3

如果你想一個事件偵聽器附加到標記直接服用,最好是做到這一點,你將它添加到地圖之後,而你仍然有一個新的參考它(如果你有可能通過添加代碼來添加代碼,這很常見)。

+2

google.maps.event.addListener(地圖, '點擊',函數(事件){ \t \t \t如果(self.marker = NULL) \t \t \t \t self.marker.setMap(NULL);! \t \t \t self.value =新google.maps.LatLng(event.latLng.lat(),event.latLng.lng()); \t \t \t \t self.marker =新google.maps.Marker({ \t \t \t \t位置:self.value \t \t \t}); \t \t \t self.zoom = map.getZoom(); \t \t \t self.marker.setMap(map); \t});上面的代碼 – user821738

+0

幫助我解決了我的問題。謝謝你的建議 – user821738