javascript - 如何计算两个纬度和经度之间的距离

标签 javascript geolocation geocoding haversine

我正在尝试使用温哥华和多伦多的纬度和经度来计算它们之间的距离。我正在使用haversine公式。我预计距离约为 4390 公里。有人可以告诉我我犯了什么错误吗?这是我的代码:

<!DOCTYPE html> 
<html> 
<head> 
<title></title>
</head>
<body onload="getDistance()">
<script>
    // Convert Degress to Radians
    function Deg2Rad( deg ) {
       return deg * Math.PI / 180;
    }

    function getDistance()
    {       
        //Toronto Latitude  43.74 and longitude  -79.37
        //Vancouver Latitude  49.25 and longitude  -123.12
        lat1 = Deg2Rad(43.74); 
        lat2 = Deg2Rad(49.25); 
        lon1 = Deg2Rad(-79.37); 
        lon2 = Deg2Rad(-123.12);
        latDiff = lat2-lat1;
        lonDiff = lon2-lon1;
        var R = 6371000; // metres
        var φ1 = lat1;
        var φ2 = lat2;
        var Δφ = Deg2Rad(latDiff);
        var Δλ = Deg2Rad(lonDiff);

        var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
                Math.cos(φ1) * Math.cos(φ2) *
                Math.sin(Δλ/2) * Math.sin(Δλ/2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

        var d = R * c;
        alert('d: ' + d);

        var dist = Math.acos( Math.sin(φ1)*Math.sin(φ2) + Math.cos(φ1)*Math.cos(φ2) * Math.cos(Δλ) ) * R;
        alert('dist: ' + dist);
    }       
 </script>
 </body>
 </html>

当我运行这段代码时,我得到了完全不同的数字。

最佳答案

我认为你应该期望更多的是 3358 公里。 http://www.distancefromto.net/distance-from/Toronto/to/Vancouver

这是一个正确结果的 fiddle :https://jsfiddle.net/Lk1du2L1/

在这种情况下,如果您从次要结果中删除 deg2rad,那么您就是正确的 - 您这样做得太频繁了:

 <!DOCTYPE html> 
<html> 
<head> 
<title></title>
</head>
<body onload="getDistance()">
<script>
    // Convert Degress to Radians
    function Deg2Rad( deg ) {
       return deg * Math.PI / 180;
    }

    function getDistance()
    {       
        //Toronto Latitude  43.74 and longitude  -79.37
        //Vancouver Latitude  49.25 and longitude  -123.12
        lat1 = Deg2Rad(43.74); 
        lat2 = Deg2Rad(49.25); 
        lon1 = Deg2Rad(-79.37); 
        lon2 = Deg2Rad(-123.12);
        latDiff = lat2-lat1;
        lonDiff = lon2-lon1;
        var R = 6371000; // metres
        var φ1 = lat1;
        var φ2 = lat2;
        var Δφ = latDiff;
        var Δλ = lonDiff;

        var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
                Math.cos(φ1) * Math.cos(φ2) *
                Math.sin(Δλ/2) * Math.sin(Δλ/2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

        var d = R * c;
        alert('d: ' + d);

        var dist = Math.acos( Math.sin(φ1)*Math.sin(φ2) + Math.cos(φ1)*Math.cos(φ2) * Math.cos(Δλ) ) * R;
        alert('dist: ' + dist);
    }       
 </script>
 </body>
 </html>

关于javascript - 如何计算两个纬度和经度之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35949220/

相关文章:

javascript - 如何在 crontab 上为 node js 项目运行 jake?

javascript - 使用内部 API 调用刷新 div

javascript - 如何让导入的 css 字体/图标对 shadow dom 中的元素产生影响?

python - 对表单提交的地址进行地理编码?

google-maps - 从地理编码器结果获取城市?

c# - Json.Net 解析不同格式的json

geolocation - 使用 Matlab 在世界地图上绘制地理位置

java - 不使用数据库快速访问数据?

javascript - 检查用户浏览器中的位置设置是否已关闭

Android Geocoder.getLocationFromName() 抛出 IOException : Service not available on Device