Options
$lat = '25.7742658';
$lng = '-80.1936589';
$miles = 30;
Query
SELECT *,
( 3959 * acos( cos( radians($lat) )
* cos( radians( lat ) )
* cos( radians( lng ) - radians($lng) )
+ sin( radians($lat) )
* sin( radians( lat ) ) ) ) AS distance
FROM locations
HAVING distance < $miles
ORDER BY distance
LIMIT 0, 20
I have a database table with 4 columns:
- unique id
- city name
- latitude (lat)
- longitude (lng)
I'm using the query on top to return locations that are within a specified number of miles from the specified coordinates. It seems to work but I'm not sure how accurate it is. I'm curios to know whether the query is good or if you have a better solution.