簡體   English   中英

如何計算兩點之間的距離?

[英]How do I calculate the distance between two points?

我有一個計算函數和latlng值,但我如何計算第一個和最后一個之間的距離?

 function calculate() {
         //poo abbreviation for point of origin not poo=shit
         var poolat = locations[0].lat;
         var poolng = locations[0].lng;
         var destlat = locations[locations.length - 1].lat;
         var destlng = locations[locations.length - 1].lng;

這個問題已在這里得到解答: 計算兩個緯度 - 經度點之間的距離? (Haversine配方)

請注意,Haversine公式假設一個完美的球體,而不是一個球體是地球的球體。

有兩種方法,你可以使用Haversine公式,它需要更多的輸入或你可以使用API​​。

在計算功能中嘗試以下操作

var distance = google.maps.geometry.spherical.computeDistanceBetween(
    new google.maps.LatLng(poolat, poolng), 
    new google.maps.LatLng(destlat, destlng)
);

的console.log(距離);

注意:

距離的默認值以米為單位。

試試這個。 內置的Math函數使其更容易

function calculate() {
     //poo abbreviation for shit
     var poolat = locations[0].lat;
     var poolng = locations[0].lng;
     var destlat = locations[locations.length - 1].lat;
     var destlng = locations[locations.length - 1].lng;

     return Math.sqrt(Math.pow((destlat - poolat),2) + Math.pow((destlng - poolng),2))
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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