假设我有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