In this blog post, you will learn how to write a MySQL latitude/longitude radius query to find the nearest location using latitude longitude MySQL. For example, if you could place yourself in the center of a city and search for the nearest ATM in a radius of 1/2 KM.
- Keep reading on Google Maps Draw Polygon Get Coordinates, MySQL find points within a radius from Database
MySQL latitude/longitude radius Query
We have stored locations (latitude and longitude) in our database. Find the following query to find the nearest location using latitude/longitude MySQL.
SELECT latitude, longitude, distance FROM (SELECT latitude, longitude, ( 6371 * Acos(Cos(Radians('22.607672')) * Cos(Radians(latitude)) * Cos( Radians(longitude) - Radians( '88.36853')) + Sin(Radians('22.607672')) * Sin(Radians(latitude))) ) AS distance FROM bts HAVING distance < '2') a ORDER BY distance DESC LIMIT 1
For more documentation refer to MySQL Docs
I hope you liked this article on finding the nearest location using latitude longitude MySQL. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.