CoderZone.org

Category: >> PHP Code >> Calculate Distance From Latitude and Llongitude Bookmark and Share

<< lastnext >>

Snippet Name: Calculate Distance From Latitude and Llongitude

Description: Provides an easy way to calculate the distance between two pairs of latitude and longitude coordinates. Output is in miles or kilometers.

Also see:
» Calculate days between any two dat...
» Calculate the difference between t...
» Date Functions: Calculate elapsed ...
» Calculate date of Easter Sunday
» Calculate TIMEDIFF with mysql vers...
» Calculate Pi
» Calculate time difference between ...
» Number Functions: Calculate ratio ...
» Calculate Zipcode Distances
» Calculate File/Folder Size In A Di...

Comment: (none)

Author: CoderZone
Language: PHP
Highlight Mode: PHP
Last Modified: February 26th, 2009

<?php
 
function distance($lat1, $lng1, $lat2, $lng2, $miles = true)
{
     $pi80 = M_PI / 180;
     $lat1 *= $pi80;
     $lng1 *= $pi80;
     $lat2 *= $pi80;
     $lng2 *= $pi80;
 
     $r = 6372.797; // mean radius of Earth in km
     $dlat = $lat2 - $lat1;
     $dlng = $lng2 - $lng1;
     $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2);
     $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
     $km = $r * $c;
 
     return ($miles ? ($km * 0.621371192) : $km);
}
 
?>
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 7