2013-05-12 105 views
0

我只能在互聯網上找到用戶的位置並將其顯示在他們的谷歌地圖上。我正在嘗試使用Google地圖提交表單。因此,用戶可以在Google地圖中選擇一個位置,並在提交按鈕的位置發送他們選擇的位置。Javascript獲取用戶在Google Maps API中的當前位置

找不到類似的東西。我知道地理編碼不是我需要的,因爲它只將地址轉換爲座標,而我需要從API本身獲取座標。

回答

0

我做了一個例子給你:http://jsfiddle.net/3vgNN/2/

獲取點擊座標系下是這樣的:

google.maps.event.addListener(map, "click", function (e) { 
     alert(e.latLng.lat() + e.latLng.lng()); 
     //optionally mark the location 
     markLocation(e.latLng); 
}); 

如果你想,你可以標記位置也:

var marker = false; 

function markLocation(userLocation){ 

if(! theLocation){ 

    marker = new google.maps.Marker({ 
     position: userLocation, 
     map: map, 
     title:"Chosen location" 
    }); 

}else{ 

    //update the marker position if it's already been set 
    marker.setPosition(userLocation); 

} 

} 

好運!

+0

這很好,但我不想點擊地圖上的任何東西。我想單擊一個HTML按鈕,它會調用一個可以獲得座標的JavaScript函數。 – 2013-05-13 01:38:56

+0

所以你想要他們選擇一個位置,然後點擊另一個按鈕來獲取所選位置的座標? – nick 2013-05-13 12:15:43

相關問題