Possible Duplicate:
Radius of multiple latitude/longitude points
distance calculations in mysql queries
select within 20 kilometers based on latitude/longitude
I have a MyISAM table, which the data is stored in. The data includes latitude and longitude for Google Maps. What I am trying to achieve is to select all objects within the radius of 40 kilometers of the sample latitude and longitude.
Thanks!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I use this query to get all points within a $radius
around $lat
/$lng
:
SELECT *, ( 6371 * acos( cos( radians({$lat}) ) * cos( radians( `lat` ) ) * cos( radians( `lng` ) - radians({$lng}) ) + sin( radians({$lat}) ) * sin( radians( `lat` ) ) ) ) AS distance FROM `positions` HAVING distance <= {$radius} ORDER BY distance ASC
6371 is the earth radius im km. And I have not invented it:
http://code.google.com/intl/en/apis/maps/articles/phpsqlsearch.html
https://developers.google.com/maps/articles/phpsqlsearch_v3#findnearsql (edit: URL changed)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0