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)