Javascript 如何在google maps api v3 javascript中点击地图的纬度、经度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8550286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get latitude, longitude onclick of a map in google maps api v3 javascript?
提问by Ramesh Kotha
how to know latitude, longitude on click of a map in google maps api v3. i have done this in google maps api v2 with this code
如何在谷歌地图api v3中点击地图知道纬度,经度。我已经使用此代码在谷歌地图 api v2 中完成了此操作
GEvent.addListener(map, "click", function(overlay, latlng) {
if (latlng) {
marker = new GMarker(latlng, {draggable:true});
GEvent.addListener(marker, "click", function() {
//alert("hello");
var html = "<table>" +
"<tr><td>Name:</td> <td><input type='text' id='name'/> </td> </tr>" +
"<tr><td>Time:</td> <td><input type='text' id='time'/> </td> </tr>" +
"<tr><td>Bus Id:</td> <td><input type='text' id='busId'/> </td> </tr>" +
"<tr><td>Device Id:</td> <td><input type='text' id='deviceId'/> </td> </tr>" +
"<tr><td></td><td><input type='button' value='Save & Close' onclick='saveData()'/></td></tr>";
marker.openInfoWindow(html);
});
map.addOverlay(marker);
}
});
how to do this same thing in v3?
如何在 v3 中做同样的事情?
i tried this but it didn't work.
我试过这个,但没有用。
google.maps.event.addListener(map, "click", function(overlay,latlng) {
if (latlng) {
marker = new google.maps.Marker(latlng);
google.maps.event.addListener(marker, "click", function() {
var html = "<table>" +
"<tr><td>Name:</td> <td><input type='text' id='name'/> </td> </tr>" +
"<tr><td>Time:</td> <td><input type='text' id='time'/> </td> </tr>" +
"<tr><td>Bus Id:</td> <td><input type='text' id='busId'/> </td> </tr>" +
"<tr><td>Device Id:</td> <td><input type='text' id='deviceId'/> </td> </tr>" +
"<tr><td></td><td><input type='button' value='Save & Close' onclick='saveData()'/></td></tr>";
marker.openInfoWindow(html);
});
map.setMap(marker);
}
});
回答by Diode
You have to use event argument.
您必须使用事件参数。
google.maps.event.addListener(map, 'click', function(event) { marker = new google.maps.Marker({position: event.latLng, map: map}); });
回答by Ashish Gupta
You have to use this.
你必须使用这个。
google.maps.event.addListener(map, 'click', function(event) {
alert("Latitude: " + event.latLng.lat() + " " + ", longitude: " + event.latLng.lng());
});