简体   繁体   English

获取谷歌地图纬度和经度坐标

[英]Getting Google Maps Lat and Long coordinates

I'm using the following code to get the coordinates of my Google maps marker as it's position is changed 我正在使用以下代码来获取位置更改后Google Maps标记的坐标

    google.maps.event.addListener(marker, 'drag', function() {
        lat = Math.round(marker.position.Ma * 1000) / 1000;
        lng = Math.round(marker.position.Na * 1000) / 1000;
        [.. using lat and long]
    });

This code used to work fine. 这段代码正常工作。 From time to time, Google changed the position variables name Ma and Na (not sure why they do so). Google有时会更改位置变量名称Ma和Na(不确定为什么这样做)。

So I'm using the right variables to get the lat and long, or is there another way of doing it? 所以我正在使用正确的变量来获取经度和纬度,或者还有另一种方法吗?

Try this: 尝试这个:

lat = Math.round(1000 * marker.getPosition().lat()) / 1000;
lng = Math.round(1000 * marker.getPosition().lng()) / 1000;

It sounds like you're using their private variables instead of accessor methods. 听起来您正在使用它们的私有变量而不是访问器方法。

I have used the following the drag event gives a MouseEvent object with latLng: 我已使用以下拖动事件为latLng提供了MouseEvent对象:

google.maps.event.addListener(marker, 'drag', function(event) {
    lat = Math.round(event.latLng.lat() * 1000) / 1000;
    lng = Math.round(event.latLng.lng() * 1000) / 1000;
    [.. using lat and long]
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM