假設我有3個位置,即加冷(A),巴耶利峇(B)和勿洛(C)。我嘗試做A-> B + B-> C,但距離總是返回A-> C。爲什麼這樣呢?計算多個位置的距離谷歌地圖
加冷至帕亞利巴爾(AB) - >2.5914199062350742公里
帕亞利巴爾到勿洛(BC) - >4.4403012109180775公里
總(ABC) - >7.03公里
加冷至勿洛( AC) - >6.88公里
下面是我的代碼:
var R = 6371;
var dLat = toRad(location3.lat()-location2.lat()) + toRad(location2.lat()-location1.lat());
var dLon = toRad(location3.lng()-location2.lng()) + toRad(location2.lng()-location1.lng());
var dLat1 = toRad(location1.lat());
var dLat2 = toRad(location2.lat());
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(dLat1) * Math.cos(dLat1) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
document.getElementById("distance_direct").innerHTML = "<br/><br/><br/>The distance between the five points (in a straight line) is: "+d +" km.";
}
function toRad(deg)
{
return deg * Math.PI/180;
}
我的加減計算語句有問題嗎?謝謝
感謝您的幫助非常好..管理做到了......非常感謝:D – SupaFlybaba