javascriptgoogle-mapsgoogle-maps-api-3

Google Maps JavaScript API LatLng Class strange behaviour


I'm using the Google Maps JavaScript API for my little Livetracking-Project. In my work I have encountered a strange behaviour with the LatLng-Class. If I input the two values shown in the example below, I always get this really strange return value from the .lng()-Method from the LatLng-Object, but not so from the .lat()-Method. I was not able to find an explanation for this behaviour in the Google documentation. Does anyone else know this issue?

var lat = 47.0189;
var lng = 8.33244;
var lastPoint = new google.maps.LatLng(lat, lng);
console.log(lastPoint.lat());
console.log(lastPoint.lng());

Which returns:

47.0189
8.33244000000002

Screenshot from JS-Console

Thanks for your help!


Solution

  • JavaScript’s numbers are usually entered as decimal floating-point numbers, but they are internally represented as binary floating-point numbers. That leads to imprecision.

    For further details you can read this book:

    http://speakingjs.com/es5/ch11.html#number_representation