In my web app i am trying to fetch longitude and latitude of a marker when i click on it. Following is the code which is setting a marker and then try to get the same marker's 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 marker i get right latitude but longitude is not same. longitude value is 13.177157360327556, original marker longitude value is 13.177157360327598. Why am i not getting right longitude? Thanks in advance.
The "error" is occurring in the constructor for the google.maps.LatLng object.
The "error" is:
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
To get the position of the marker (not the click), use marker_obj.getPosition() not event.latLng.
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:
you will see, that
lng
andmyLatLng.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.