sonny

10-11-2012, 03:57 AM

Hi

Can someone please check this, the miles do not seem right when I test it

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);

}

$lat1 = "39.9289";//Beijing China

$lon1 = "116.3883";

$lat2 = "47.6026";//Seattle Washington

$lon2 = "-122.3284";

$distance = distance($lat1, $lon1, $lat2, $lon2);

echo "They are ".$distance." miles away from each other

Can someone please check this, the miles do not seem right when I test it

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);

}

$lat1 = "39.9289";//Beijing China

$lon1 = "116.3883";

$lat2 = "47.6026";//Seattle Washington

$lon2 = "-122.3284";

$distance = distance($lat1, $lon1, $lat2, $lon2);

echo "They are ".$distance." miles away from each other