2013-05-12 109 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

相关问题