Why am i not getting exact Longitude when i click on a marker?

我与影子孤独终老i 提交于 2019-11-27 16:15:01

To get the position of the marker (not the click), use marker_obj.getPosition() not event.latLng.

google.maps.event.addListener(marker_obj, "click", function(event) {
    var myLatLng = marker_obj.getPosition();
    var lat1 = myLatLng.lat();
    var lng1 = myLatLng.lng();
    console.log('marker latitude: '+lat1);
    console.log('marker longitude: '+lng1);

    $.ajax({
        type: 'POST',
        url: 'http://localhost:8888/action',
        data: { latitude: lat1, longitude: lng1},
        success: function(result) {
            console.log(result);
        }
    });
});

I would call it a bug.

It's not an issue of the marker or the event, it already happens when you create the LatLng-object.

Try this:

var lat=55.68322317670628,
     lng=13.177157360327598,
     myLatLng = new google.maps.LatLng(lat,lng);

you will see, that lng and myLatLng.lng() have different values.

http://jsfiddle.net/doktormolle/Jm9Ww/

As a workaround I would suggest to use less accurate values with a precision of 6-10 decimals.

The "error" is occurring in the constructor for the google.maps.LatLng object.

var latitude = 55.68322317670628;
var longitude = 13.177157360327598;
var myLatlng = new google.maps.LatLng(latitude, longitude);

var longitude_diff = longitude-myLatlng.lng();

The "error" is:

longitude diff: 4.263256414560601e-14 degrees
longitude diff: 2.681678819840079e-9 meters  (2.68 nanometers)

Probably not relevant to anything in the real world that you would use a map for.

example calculation Longitude degrees to meters calculation from this page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!