Last active
August 29, 2015 14:03
-
-
Save germain-gg/9ad4234b0ff6ee4a77f2 to your computer and use it in GitHub Desktop.
PHP Function to check if a latitude/longitude point is nearer than a distance thresold
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 | |
/* | |
* Check if the tournament is nearer than the distance thresold | |
* according to the latitude and longitude parameters | |
* | |
* param1 : $latitude of the user | |
* param2 : $longitude of the user | |
* param3 : max distance | |
* | |
* Formula : http://www.movable-type.co.uk/scripts/latlong.html | |
*/ | |
public function isNearerThan($latitude, $longitude, $distanceThresold) { | |
$earthRadius = 6371; | |
$dLat = deg2rad($latitude - $this->latitude); | |
$dLon = deg2rad($longitude - $this->longitude); | |
$lat1 = deg2rad($this->latitude); | |
$lat2 = deg2rad($latitude); | |
$a = sin($dLat / 2) * sin($dLat / 2) + | |
sin($dLon / 2) * sin($dLon / 2) * cos($lat1) * cos($lat2); | |
$c = 2 * atan2( sqrt($a) , sqrt(1-$a) ); | |
$distance = $earthRadius * $c; | |
return ($distance <= $distanceThresold) ? true : false; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment