• 0

distance between latitude and longitude


Question

hi guys i have a problem this function


function distance($lat1, $lng1, $lat2, $lng2, $miles = true)
{
$pi80 = M_PI / 180;
$lat1 *= $pi80;
$lng1 *= $pi80;
$lat2 *= $pi80;
$lng2 *= $pi80;

$r = 6372.797; // mean radius of Earth in km
$dlat = $lat2 - $lat1;
$dlng = $lng2 - $lng1;
$a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
$km = $r * $c;

return ($miles ? ($km * 0.621371192) : $km);
}
[/CODE]

now if i call it like this

[CODE]
distance("40.459202", "74.302002", "51.549999", "0.25");
[/CODE]

the output is 5296.7.... Miles

if i switch the postions like so

[CODE]
distance("51.549999", "0.25","40.459202", "74.302002");
[/CODE]

the output is 2045.5.... Miles

NOW i know the acutal distance is about 3500 miles ish but why is the distance differant between the 2 calls?

thanks

1 answer to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.