【问题标题】:How to calculate the distance between two latitude and longitude如何计算两个经纬度之间的距离
【发布时间】:2016-06-27 05:30:30
【问题描述】:

我正在尝试使用经纬度计算温哥华和多伦多之间的距离。我正在使用 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>

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

【问题讨论】:

标签: javascript geolocation geocoding haversine


【解决方案1】:

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

这是一个正确结果的小提琴: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>

【讨论】:

  • 我知道,我找不到我在代码中犯的错误。当我运行代码时,我得到一个完全不同的数字 85568.6616。
  • 查看我发布的代码。当我用这些更改运行你的代码时,我得到了正确的距离:d: 3355945.3482
  • 您在创建 lat1 & lat2 等变量时已经转换了 deg2rad。当你再次这样做时,你正在改变你的价值观并得到不正确的85568.661号码。
  • 我更改了我的代码。删除了重复项,现在我得到 dist: 615456.6054867743 这看起来也不正确。
  • 是的,希腊字符正在制造麻烦。当我用它工作的正常字符替换它时。
猜你喜欢
  • 2018-06-11
  • 2012-10-13
  • 2017-09-01
  • 2017-01-26
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
  • 2014-04-10
  • 1970-01-01
相关资源
最近更新 更多