相关疑难解决方法(0)

计算两个纬度 - 经度点之间的距离?(Haversine配方)

如何计算纬度和经度指定的两点之间的距离?

为了澄清,我想要以公里为单位的距离; 这些要点使用WGS84系统,我想了解可用方法的相对准确性.

algorithm math maps latitude-longitude haversine

852
推荐指数
19
解决办法
72万
查看次数

在Javascript中使用Haversine公式

我正在尝试使用Haversine距离公式(如此处所示:http://www.movable-type.co.uk/scripts/latlong.html)但我无法使其工作,请参阅以下代码

    function test() { 
    var lat2 = 42.741; 
    var lon2 = -71.3161; 
    var lat1 = 42.806911; 
    var lon1 = -71.290611; 

    var R = 6371; // km 
    //has a problem with the .toRad() method below.
    var dLat = (lat2-lat1).toRad();  
    var dLon = (lon2-lon1).toRad();  
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
                    Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
                    Math.sin(dLon/2) * Math.sin(dLon/2);  
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c; 

    alert(d); 
}
Run Code Online (Sandbox Code Playgroud)

错误是:

Uncaught TypeError: Object -0.06591099999999983 …
Run Code Online (Sandbox Code Playgroud)

javascript haversine

29
推荐指数
4
解决办法
3万
查看次数