使用Haversine_formula获得两点之间的距离

时间:2014-01-26 18:29:18

标签: php geolocation geo

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

我使用了Ahmedab​​ad,Banglore的协调点。 距离差异应该接近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";
}

3 个答案:

答案 0 :(得分:1)

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

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

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

相反。

答案 1 :(得分: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 :(得分:1)

以下是作为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;
}
?>