I have a strange problem using the Cosine formula in my PHP application..
function CalculateDistanceCosine($decA, $decB)
{
$lon1 = $decA[0]; //This would be equal to point A's longitude, and so on..
$lat1 = $decA[1];
$lon2 = $decB[0];
$lat2 = $decB[1];
//echo $lon1." ".$lat1."<br/>";
//echo $lon2." ".$lat2."<br/>";
$distance = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon2-$lon1));
$distance = acos($distance);
$distance = rad2deg($distance);
$distance = $distance * 60 * 1.1515;
$distance = round($distance, 4);
return $distance;
}
My input into this would be something like this:
45.468055555556 -73.741388888889 //- The coordinates for Montreal International Airport
28.428888888889 -81.315833333333 //- Orlando International Airport
However, upon using it, I get wild mistakes.. i.e, "The distance from Montreal to Orlando is 576 KM -- very wrong."
What's interesting is that it is very accurate along the longitude axis. For example, if I gave an input of:
50 -73.741388888889
50 -81.315833333333
The error is now only about 50KM, very acceptable.
In other words, why is it neglecting latitudinal differences?
I've tried the Harvesine formula with similar results unfortunately.