You just need to use the following query.
For example, you have the latitude and longitude of entering 37 and -122 in degrees. And you want to search for users within a radius of 25 miles from the current given latitude and longitude.
SELECT item1, item2, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM geocodeTable HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;
If you want the search distance in km, replace 3959 with 6371 in the query above.
You can also do this, for example:
Select all latitude and longitude
Then calculate the distance for each record.
The above process may be performed with multiple redirection.
You can use a stored procedure to optimize the request.
And this can also help you.
Gaurav
source share