Reputation: 41
I have the following code in my script. I would like to limit the number to about 6 digits past the decimal returned on the pin marker. Currently I get about 15 digits past the decimal.
var infowindow = new google.maps.InfoWindow({
content: + location.lat() + ',' + location.lng()
// ...
});
Upvotes: 3
Views: 4115
Reputation: 28860
If you want to always have six digits after the decimal, even if they are zeroes, you could do:
var infowindow = new google.maps.InfoWindow({
content:
location.lat().toFixed(6) +
',' +
location.lng().toFixed(6)
});
Or if you want to remove trailing zeros and also remove the decimal point if the value is a whole number, you could do this:
function formatDecimal( number, precision ) {
return number.toFixed( precision ).replace( /[\.]?0+$/, '' );
}
var infowindow = new google.maps.InfoWindow({
content:
formatDecimal( location.lat(), 6 ) +
',' +
formatDecimal( location.lng(), 6 )
});
BTW, this is a JavaScript question, not a Google Maps API question. location.lat()
and location.lng()
are JavaScript numbers, so you use JavaScript code to format them as you like. (I'm mentioning this only because it will help you find answers to your coding questions faster.)
Upvotes: 5