Rasher's Toolbox

Back

lat_lon_dist

int lat_lon_dist(lat1, lon1, lat2, lon2 [, emperic]) -- Calculate the distance between two coordinates.

Description

Calculates the distance between the two coordinates defined by lat1,lon1 and lat2,lon2. Returns in km unless the optional fifth parameter is set to true, in which case the result will be returned in miles. The function was found on http://sourceforge.net/snippet/detail.php?type=snippet&id=100060 and the fifth parameter added by me.

Example

<?php
  
echo "Distance from New York to Paris is ".lat_lon_dist(48.86672.333340.7000, -74.0000)."km.";
?>

This will output:

Distance from New York to Paris is 5828.092745214km.

Source

Published under the terms of the GPL

<?php
function lat_lon_dist($lat1$lon1$lat2$lon2$emperic false) {
    
$dist acos(sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon1 $lon2)));
    
$dist rad2deg($dist);
    
$miles $dist 69;
    if (
$emperic !== false) { return $miles; }
    else { return 
$miles 1.609344; }
}
?>

Last updated: Sat Jun 14 20:02:45 CEST 2008

Valid XHTML 1.0! Valid HTML 3.2!