Skip to content

Instantly share code, notes, and snippets.

@germain-gg
Last active August 29, 2015 14:03
Show Gist options
  • Save germain-gg/9ad4234b0ff6ee4a77f2 to your computer and use it in GitHub Desktop.
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
<?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