Skip to content

Instantly share code, notes, and snippets.

@revolunet
Created April 26, 2012 11:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save revolunet/2498903 to your computer and use it in GitHub Desktop.
Save revolunet/2498903 to your computer and use it in GitHub Desktop.
sql based geolocation query
#
# geolocate points 50km around a defined latitude/longitude
#
$latitude = 42.45679;
$longitude = 8.345678;
$sql = "SELECT ID, 3956 * 2 * ASIN(SQRT(POWER(SIN((" . $latitude . " - LAT) * 0.0174532925 / 2), 2) + ";
$sql .= "COS(" . $latitude . " * 0.0174532925) * COS(LAT * 0.0174532925) * POWER(SIN((" . $longitude . " - LON) * 0.0174532925 / 2), 2) ";
$sql .= ")) as DISTANCE from houses having DISTANCE < 50 ORDER BY DISTANCE ASC ";
@revolunet
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment