Mysql in request distance

Functions

$lat = '25.7742658';
$lng = '-80.1936589';
$miles = 30;

Inquiry

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 identificator
  • city ​​name
  • latitude (lat)
  • longitude (lng)

I use the query from above to return places that are within a given number of miles from the specified coordinates. It seems to work, but I'm not sure how accurate it is. I am curious to know if the request is good or if you have a better solution.

+5
source share
2 answers

which looks like a valid distance request in a big circle.

What are you worried about wrt accuracy?

+3
source

All Articles