Created
December 9, 2010 04:04
-
-
Save samir/734321 to your computer and use it in GitHub Desktop.
Distance between two coordinates (latitude/longitude) in PHP
This file contains 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
function distance($lat1 = 0, $lng1 = 0, $lat2 = 0, $lng2 = 0, $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); | |
} | |
# Source: http://snipplr.com/view/2531/calculate-the-distance-between-two-coordinates-latitude-longitude/ | |
# Reference for others languages: http://www.codecodex.com/wiki/Calculate_distance_between_two_points_on_a_globe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
please i am new to programming
please, where do i insert the two coordinates and how do i see the results. a working example will go a long way. thanks.