簡體   English   中英

限制Google API Map中經度和緯度返回的小數點數

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM