Basit Basit - 1 month ago 15
MySQL Question

latitude/longitude find nearest latitude/longitude - complex sql or complex calculation

I have latitude and longitude and I want to pull the record from the database, which has nearest latitude and longitude by the distance, if that distance gets longer than specified one, then don't retrieve it.

Table structure:

id
latitude
longitude
place name
city
country
state
zip
sealevel

Answer

What you need is to translate the distance into degrees of longitude and latitude, filter based on those to bound the entries that are roughly in the bounding box, then do a more precise distance filter. Here is great paper that explains how to do all this:

http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL