2012
10.11

If you have a bunch of locations (with long/lat coordinates) in a DB, it might be useful for the user to find the nearest location to their current location. You can do this with MySQL using this query.

SELECT *, 
((ACOS(SIN($lat * PI() / 180) * SIN(`Lat` * PI() / 180) + COS($lat * PI() / 180) * COS(`Lat` * PI() / 180) * COS(($lon - `Long`) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS `distance`
FROM `tblLocations`
HAVING `distance`<='25'
ORDER BY `distance`ASC

You might want to LIMIT 1 too, but you get the idea.

No Comment.

Add Your Comment