Skip to content

Instantly share code, notes, and snippets.

@henriqueramos
Last active January 28, 2020 18:32
Show Gist options
  • Save henriqueramos/2cb8bf6d9648e76e95e0a33adbab6f7b to your computer and use it in GitHub Desktop.
Save henriqueramos/2cb8bf6d9648e76e95e0a33adbab6f7b to your computer and use it in GitHub Desktop.
Find points (a.k.a users) within a distance using MySQL
# :lat as your requested latitude
# :lon as your requested longitude
# :r as your Earth's radius - Could be 6371 for KM or 3959 for miles
# :rad as your radius search distance
SELECT
`userId`,
`name`,
`address`,
AsText(`spatialData`) AS 'spatialData',
acos(sin(:lat) * sin(radians(X(`spatialData`))) + cos(:lat)*cos(radians(X(`spatialData`)))*cos(radians(Y(`espacial`))-:lon))*:r AS 'Distance',
X(`spatialData`) AS 'lat',
Y(`spatialData`) AS 'lon'
FROM
`users`
WHERE
Distance <= :rad
ORDER BY Distance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment