Last active
September 25, 2015 15:42
-
-
Save victordit/2884a2b91cd19e820d3c to your computer and use it in GitHub Desktop.
Calc the radius (distance) between two points (2 latitudes and 2 longitudes)
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
<?php | |
/** | |
* @return integer | |
* @param double $lat_from latitude | |
* @param double $lon_from longitude | |
* @param double $lat_to latitude | |
* @param double $lon_to longitude | |
* @param string $unit The format of return ('km' =>kilometers,'mts'=>meters ) | |
* | |
*/ | |
public function getRadius($lat_from,$lon_from,$lat_to,$lon_to,$unit="km") | |
{ | |
$R = 6371; // km (change this constant to get miles) | |
$dLat = ($lat_to-$lat_from) * M_PI / 180; | |
$dLon = ($lon_to-$lon_from) * M_PI / 180; | |
$a = sin($dLat/2) * | |
sin($dLat/2) + cos($lat_from * M_PI / 180 ) * | |
cos($lat_to * M_PI / 180 ) * sin($dLon/2) * sin($dLon/2); | |
$c = 2 * atan2(sqrt($a),sqrt(1-$a)); | |
$d = $R * $c; | |
if($unit == 'mts'){ | |
return round($d*1000);//."m"; | |
}else{ | |
return round($d,3);//."km"; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment