javascript 纬度、经度抓取器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5571641/
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
Latitude, Longitude Grabber
提问by Mat B.
What I would like to do is create app where when anyone clicks on the map object and two fields get filled with latitude and longitude at every click. Is there sample code of Google Maps V3 JavaScript API (Geocoding API) somewhere out there that does something like that?
我想做的是创建应用程序,当任何人单击地图对象时,每次单击时两个字段都会填充纬度和经度。是否有 Google Maps V3 JavaScript API(地理编码 API)的示例代码可以做类似的事情?
回答by u476945
You can achieve this with google.maps.event'LatLng property:
您可以使用google.maps.event'LatLng 属性实现此目的:
Something like this would show how it works. Here is a JSFiddle Demo:
像这样的东西会显示它是如何工作的。这是一个JSFiddle 演示:
google.maps.event.addListener(map, 'click', function(event){
alert('Lat: ' + event.latLng.lat() + ' Lng: ' + event.latLng.lng());
});
Where map is your google map object. Yes the example is in V2 i think, and if you would like to create a marker on click you can simply create it within the click callback function.
其中 map 是您的 google map 对象。是的,我认为这个例子在 V2 中,如果你想在点击时创建一个标记,你可以简单地在点击回调函数中创建它。
回答by Jayapal Chandran
Here is a complete code for version 2 which i had been using for a long time and i have published in the site.
这是第 2 版的完整代码,我已经使用了很长时间,并且已在网站上发布。
just click view source code from your browser and copy save the contents. it will work perfectly. only thing is you need to change the map api key.
只需单击浏览器中的查看源代码并复制保存内容。它会完美地工作。唯一的问题是您需要更改地图 api 密钥。
Here is the code for google map version 3
这是谷歌地图版本 3 的代码
回答by Gourav khanna
function onload() { var map= new google.maps.Map(document.getElementById('map'), { center: new google.maps.LatLng(37.4419, -122.1419), zoom: 14, mapTypeId: google.maps.MapTypeId.ROADMAP, disableDefaultUI: true }); google.maps.event.addListener(map, 'click', function(event){ alert('Lat: ' + event.latLng.lat() + ' and Longitude is: ' + event.latLng.lng()); }
After loading code you can write this code to get latitude and longitude of fix points. please try
加载代码后,您可以编写此代码来获取固定点的纬度和经度。请试试
回答by James
var map = [google map];
google.maps.event.addListener(map, 'click', function (mouseEvent) {
alert(mouseEvent.latLng.toUrlValue());
});