« Bash Patch to log to mySQL | Main | Webapp from scratch the right way part 1 »

Calculate Distance In Mysql with Latitude and Longitude

So you have a whole table full of members or places with latitude and longitude's associated with them. Just replace the $lat and $lon with the center point you want to find distances from. You can also change the distance<=10 to a number you want to search from. This will limit your results to all results that are under 10 miles from the starting point

SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * COS(($lon - lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance FROM members HAVING distance<='10' ORDER BY distance ASC

TrackBack

TrackBack URL for this entry:
http://www.zcentric.com/cgi-bin/mt-tb.cgi/13

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on March 9, 2007 10:25 PM.

The previous post in this blog was Bash Patch to log to mySQL.

The next post in this blog is Webapp from scratch the right way part 1.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33