Skip to content

Instantly share code, notes, and snippets.

@mosufy
Last active August 29, 2015 14:05
Show Gist options
  • Save mosufy/1779c192474bf8de887e to your computer and use it in GitHub Desktop.
Save mosufy/1779c192474bf8de887e to your computer and use it in GitHub Desktop.
Fetch places of interest within radial distance using Haversine formula.

MySQL Nearby Radial Distance

Fetch places of interest within $dist (radial distance) of $fLat (latitude) and $fLon (longitude) using Haversine formula.

SELECT name,
  ACOS( SIN( RADIANS( `lat` ) ) * SIN( RADIANS( $fLat ) ) + COS( RADIANS( `lat` ) )
  * COS( RADIANS( $fLat )) * COS( RADIANS( `lon` ) - RADIANS( $fLon )) ) * 6380 AS `distance`
FROM `places_of_interests`
WHERE
  ACOS( SIN( RADIANS( `lat` ) ) * SIN( RADIANS( $fLat ) ) + COS( RADIANS( `lat` ) )
  * COS( RADIANS( $fLat )) * COS( RADIANS( `lon` ) - RADIANS( $fLon )) ) * 6380 < $dist
ORDER BY `distance`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment