tom66

October 29th, 2009, 01:03 AM

I'm trying to calculate a distance from two latitude and longitude positions. However, I keep getting strange 4,000 km+ results, when I should be getting around 11 km.

I'm using the following PHP function:

function estimate_distance_crow_flies($lat1, $lon1, $lat2, $lon2)

{

return acos(sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon2 - $lon1))) * 6371;

}

With the following values:

Lat Long

0.313611 32.581111 Kampala, Uganda

0.397222 32.638889 Kira, Uganda

I can verify it from the internet, that it should be about 11 km:

http://www.movable-type.co.uk/scripts/latlong.html

(and also a map and a ruler)

So I suspect something's wrong with my formula, but what? This error occurs for different values, as well.

Help much appreciated...

I'm using the following PHP function:

function estimate_distance_crow_flies($lat1, $lon1, $lat2, $lon2)

{

return acos(sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon2 - $lon1))) * 6371;

}

With the following values:

Lat Long

0.313611 32.581111 Kampala, Uganda

0.397222 32.638889 Kira, Uganda

I can verify it from the internet, that it should be about 11 km:

http://www.movable-type.co.uk/scripts/latlong.html

(and also a map and a ruler)

So I suspect something's wrong with my formula, but what? This error occurs for different values, as well.

Help much appreciated...