gpt4 book ai didi

javascript - 限制谷歌API map 中经纬度返回的小数点数

转载 作者:行者123 更新时间:2023-11-29 10:15:08 25 4
gpt4 key购买 nike

我的脚本中有以下代码。我想将数字限制在引脚标记返回的小数点后约 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 代码根据需要格式化它们。 (我提到这一点只是因为它可以帮助您更快地找到编码问题的答案。)

关于javascript - 限制谷歌API map 中经纬度返回的小数点数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23850773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com