| px | top | add code | search | signup | login | help |
<?php
/* assume your points, in decimal, are in $lon1,$lat1 and $lon2,$lat2
* this can also be useful with the US ZIPcodes at
* http://ftp.census.gov/geo/www/gazetteer/places.html
*/
$pi = 3.1415926;
$rad = doubleval($pi/180.0);
$lon1 = doubleval($lon1)*$rad; $lat1 = doubleval($lat1)*$rad;
$lon2 = doubleval($lon2)*$rad; $lat2 = doubleval($lat2)*$rad;
$theta = $lon2 - $lon1;
$dist = acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($theta));
if ($dist < 0) { $dist += $pi; }
$dist = $dist * 6371.2;
$miles = doubleval($dist * 0.621);
$inches = doubleval($miles*63360);
$dist = sprintf("%.2f",$dist);
$miles = sprintf("%.2f",$miles);
$inches = sprintf("%.2f",$inches);
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.