Created
August 6, 2012 15:16
-
-
Save lamprosg/3275343 to your computer and use it in GitHub Desktop.
Distance between 2 points, with lat and lng coordinates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php function distance($lat1, $lon1, $lat2, $lon2) { | |
| $pi80 = M_PI / 180; | |
| $lat1 *= $pi80; | |
| $lon1 *= $pi80; | |
| $lat2 *= $pi80; | |
| $lon2 *= $pi80; | |
| $r = 6372.797; // mean radius of Earth in km | |
| $dlat = $lat2 - $lat1; | |
| $dlon = $lon2 - $lon1; | |
| $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlon / 2) * sin($dlon / 2); | |
| $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); | |
| $km = $r * $c; | |
| //echo '<br/>'.$km; | |
| return $km; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment