[英]Limit the number Of Decimal Points Returned on Longitude and Latitude in Google API Map
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.
我想将数字限制在引脚标记返回的小数点后约6位数。 Currently I get about 15 digits past the decimal.
目前我得到小数点后的大约15位数。
var infowindow = new google.maps.InfoWindow({
content: + location.lat() + ',' + location.lng()
// ...
});
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. 顺便说一句,这是一个JavaScript问题,而不是Google Maps API问题。
location.lat()
and location.lng()
are JavaScript numbers, so you use JavaScript code to format them as you like. location.lat()
和location.lng()
是JavaScript编号,因此您可以根据需要使用JavaScript代码对其进行格式化。 (I'm mentioning this only because it will help you find answers to your coding questions faster.) (我之所以提到这一点,只是因为它可以帮助您更快地找到编码问题的答案。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.