Reputation: 3785
I am trying to write a php function that calculates proximity between two points but I cant get it to work.
I am currently using a function found here: http://www.zipcodeworld.com/samples/distance.php.html
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "m") {
return ($miles * 1.609344)/1000;
}
else if ($unit == "N") {
return ($miles * 0.8684);
}
else {
return $miles;
}
}
My application requires precision down to meters but no matter what I do I dont get correct results.
I get the coordinates from here: http://itouchmap.com/latlong.html
What am I doing wrong? Can you please help me out with this?
Thanks
Mike
Upvotes: 0
Views: 2635
Reputation: 21553
I wrote this little class a while ago now, but it should help you with better distance calculations than the method in your question: https://github.com/treffynnon/Geographic-Calculations-in-PHP
To complete your question with the above link would be:
$Geography = new Geography();
echo $Geography->getDistance($lat1, $lon1, $lat2, $lon2);
Will give the distance in metres using Vincenty's formulae for the distance calculation.
The Vincenty's formulae used in my class is a PHP port of the javascript function available at http://www.movable-type.co.uk/scripts/LatLongVincenty.html
Upvotes: 3
Reputation: 210
Here's a page with great detail on the algorithms available with JS examples: http://www.movable-type.co.uk/scripts/latlong.html
Upvotes: 0