$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
getting distance between two point using Haversine_formula
-
03-10-2022 - |
سؤال
Here is my code, It gives result .
I have used co-ordinate points for Ahmedabad , Banglore. Distance diff should be near to 1500 but here I get 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";
}
المحلول
نصائح أخرى
$a = pow(sin($delta_lat/2), 2);
That is still in degrees, so you should use
$a = pow(sin(deg2rad($delta_lat)/2), 2);
Instead.
Below is the Haversine formulae as a PHP function. I keep a version of it online for download:
http://www.opengeocode.org/download/haversine/haversine.php.txt
If you want miles, change 6372.8 to 3959. Also, you can find a lot of discussion on distant calculation on the closed question: 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;
}
?>
لا تنتمي إلى StackOverflow