开发者

Mysql within distance query

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 sur开发者_StackOverflow中文版e how accurate it is. I'm curios to know whether the query is good or if you have a better solution.


that looks like the correct great circle distance query.

what are you concerned with wrt accuracy?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜