[英]Limit the number Of Decimal Points Returned on Longitude and Latitude in Google API Map
我的脚本中有以下代码。 我想将数字限制在引脚标记返回的小数点后约6位数。 目前我得到小数点后的大约15位数。
var infowindow = new google.maps.InfoWindow({
content: + location.lat() + ',' + location.lng()
// ...
});
如果你想在小数点后总是有六位数,即使它们是零,你可以这样做:
var infowindow = new google.maps.InfoWindow({
content:
location.lat().toFixed(6) +
',' +
location.lng().toFixed(6)
});
或者,如果要删除尾随零,并且如果值是整数,则还删除小数点,您可以这样做:
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 )
});
顺便说一句,这是一个JavaScript问题,而不是Google Maps API问题。 location.lat()
和location.lng()
是JavaScript编号,因此您可以根据需要使用JavaScript代码对其进行格式化。 (我之所以提到这一点,只是因为它可以帮助您更快地找到编码问题的答案。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.