Why don't I get the exact longitude when I click on the marker?

In my web application, I am trying to get the longitude and latitude of the marker when I click on it. Below is the code that sets the marker, and then try to get the same mark of latitude and longitude when I click on it:

var myLatlng = new google.maps.LatLng(55.68322317670628, 13.177157360327598); var marker_obj = new google.maps.Marker({ clickable: true, position: myLatlng, map: dashboard_map, zIndex: i }); marker_obj.setIcon('marker.png'); google.maps.event.addListener(marker_obj, "click", function(event) { var myLatLng = event.latLng; 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); } }); }); 

When I click on the marker, I get the correct latitude, but the longitude is not the same. the longitude value is 13.177157360327556, the marker longitude value is 13.177157360327598. Why am I not getting the correct longitude? Thanks in advance.

+4
source share
3 answers

To get the marker position (rather than a click), use the marker_obj.getPosition () not event.latLng function.

 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); } }); }); 
+2
source

I would call it a mistake.

This is not a marker or event problem; it already occurs when creating a LatLng object.

Try the following:

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

You will see that lng and myLatLng.lng() have different meanings.

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

As a workaround, I would suggest using less accurate values ​​accurate to 6-10 decimal places.

0
source

"Error" occurs in the constructor of google.maps.LatLng.

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

"Error":

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

It probably has nothing to do with anything in the real world that you would use a map.

calculation example Calculation of longitude in degrees per meter from this page

0
source

Source: https://habr.com/ru/post/1445035/


All Articles