【问题标题】:getting distance between two point using Haversine_formula使用 Haversine_formula 获取两点之间的距离
【发布时间】:2014-02-17 12:02:05
【问题描述】:

这是我的代码,它给出了结果。

我使用了班格罗尔艾哈迈达巴德的坐标点。 距离差异应该接近 1500,但在这里我得到 32200

function points{
      $lat_a = 12.96;
        $lon_a = 77.56;
        $lat_b = 23.03;
        $lon_b = 72.58;
        $earth_radius = 6372.795477598;
      $delta_lat = $lat_b - $lat_a ;
      $delta_lon = $lon_b - $lon_a ;
      $a = pow(sin($delta_lat/2), 2);
      $a += cos(deg2rad($lat_a)) * cos(deg2rad($lat_b)) * pow(sin(deg2rad($delta_lon/29)), 2);
      $c = 2 * atan2(sqrt($a), sqrt(1-$a));
      $distance = 2 * $earth_radius * $c;
      $distance = round($distance, 4);
        echo "<br/>dist $distance";
}

【问题讨论】:

  • 距离应该是929.5 英里,你用的是什么UOM?
  • sin() 和 cos() 期望以弧度输入;您正在使用初始化 $a 的度数
  • pow(sin(deg2rad($delta_lon/29)), 2)...29从何而来?
  • @MarkBaker:正确指出。让我检查一下

标签: php geolocation geo


【解决方案1】:
$lat_a = 12.96;
$lon_a = 77.56;
$lat_b = 23.03;
$lon_b = 72.58;

$earth_radius = 6372.795477598;

$delta_lat = $lat_b - $lat_a ;
$delta_lon = $lon_b - $lon_a ;

$a = pow(sin(deg2rad($delta_lat/2)), 2) + cos(deg2rad($lat_a)) * cos(deg2rad($lat_b)) * pow(sin(deg2rad($delta_lon/2)), 2);
$c = 2 * asin(sqrt($a));
$distance = $earth_radius * $c;
$distance = round($distance, 4);

echo "<br/>dist $distance"; // dist 1237.3685

【讨论】:

  • 你能给我你用过的参考吗?
【解决方案2】:
$a = pow(sin($delta_lat/2), 2); 

那仍然是度数,所以你应该使用

$a = pow(sin(deg2rad($delta_lat)/2), 2); 

相反。

【讨论】:

    【解决方案3】:

    下面是作为 PHP 函数的 Haversine 公式。我在网上保留了一个版本以供下载:

    http://www.opengeocode.org/download/haversine/haversine.php.txt

    如果你想要里程,将6372.8改为3959。另外,你可以在封闭的问题上找到很多关于远程计算的讨论:MySQL Great Circle Distance (Haversine formula)

    <?php
    // Get Distance between two lat/lng points using the Haversine function
    // First published by Roger Sinnott in Sky & Telescope magazine in 1984 (“Virtues of the Haversine”)
    //
    function Haversine( $lat1, $lon1, $lat2, $lon2) 
    {
        $R = 6372.8;    // Radius of the Earth in Km
    
        // Convert degress to radians and get the distance between the latitude and longitude pairs
        $dLat = deg2rad($lat2 - $lat1);
        $dLon = deg2rad($lon2 - $lon1);
    
        // Calculate the angle between the points
        $a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin($dLon/2) * sin($dLon/2);
        $c = 2 * asin(sqrt($a));
        $d = $R * $c;
    
        // Distance in Kilometers
        return $d;
    }
    ?>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-28
    • 2014-01-21
    • 2011-02-14
    • 2020-12-22
    • 2021-11-03
    • 2015-05-14
    • 2013-02-06
    • 1970-01-01
    相关资源
    最近更新 更多