Quantcast
Channel: Adam Zwakk » Tutorials
Viewing all articles
Browse latest Browse all 10

Snippet: Nearest Location Finder using MySQL

$
0
0

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.


Viewing all articles
Browse latest Browse all 10

Trending Articles