Get a list of user who are inside the circle or square of a given latitude and longitude with given distance or radius.
For example my input Lat= 75.1545 and Long = 64.7643 and distance = 10 miles. I would like to get the list of users who are inside 10 miles distance from the point 75.1545 and 64.7643. Now its possible to solve this by single Mysql query.
So we need a user table with user name, latitude and longitude of the users address.
The Mysql query that will find the closest 50 locations that are within a radius of 10 miles to the 75.1545, 64.7643 coordinate. This Query will calculates the distance based on the latitude/longitude of that row and the target lat/long, and then asks for only rows where the distance value is less than 10 miles, orders the whole query by distance, and limits it to 50 results.
To search by kilometers instead of miles, replace 3959 with 6371.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
SELECT user-id, ( 3959 * acos ( cos ( radians(75.1545) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(64.7643) ) + sin ( radians(75.1545) ) * sin( radians( lat ) ) ) ) AS user_distance FROM users HAVING user_distance< 10 ORDER BY user_distanceASC LIMIT 0 , 50; |