按纬度和经度查找最近的地点
我有一个存储某些地点的纬度和经度的网站,我想显示特定地点最近的地点(比如说,半径为 10 公里)。
如何在我的 MySQL 表中搜索这些位置?
I have this website that stores latitude and longitude of some places and I want to display the nearest places (lets say, in a radius of 10km) of a specific place.
How do I search on my MySQL table for these places?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几年前,我在 Google 问答中回答过此类问题,最近又在Uclue。
我应该指出,首选的技术惯例是将西方的经度表示为负值,但有时没有观察到这一点。除非您计算的是与本初子午线相反的子午线(经度环绕的地方),否则不会有太大区别,但需要注意您的数据库以及与其他地理存储库的一致性。
补充:如上面链接的 Uclue 问题中所述,目的是计算两对值,MINLATITUDE/MAXLATITUDE 和 MINLONGITUDE/MAXLONGITUDE,以便包含给定位置的给定距离内的所有点在 SQL 查询的结果中:
从 MySQL 5.0 开始,我们可以将 MIN/MAX 对的计算与 SELECT 一起捆绑在一个方便的存储过程中。我们甚至可以使用光标遍历此类查询的结果,并检查确切的“大圆”距离(如果需要这种精度)。不过,我猜想,从 Python 或 C++ 等调用框架动态构建上述查询的简化方法对于大多数应用程序来说都是有意义的(为了最大限度地减少数据库服务器所做的工作)。
让我知道我是否应该详细说明这些可能性。
I answered this kind of question some years ago at Google Answers and again more recently at Uclue.
I should note that the preferred technical convention is to represent longitudes in the West as negative values, but this is sometimes not observed. It won't make much difference unless you are calculating across the meridian opposite to the Prime Meridian (where longitude wraps around), but it is something to be aware of about your database and consistency with other geographic repositories.
Added: As outlined in the Uclue question linked above, the aim is to compute two pairs of values, MINLATITUDE/MAXLATITUDE and MINLONGITUDE/MAXLONGITUDE, so that all points within a given distance of a given location are included in the results of SQL query:
Starting with MySQL 5.0 we can bundle the computations of the MIN/MAX pairs together with the SELECT in a convenient stored procedure. We can even run through the results of such a query with a cursor, and check the exact "great circle" distance if such precision is desired. However I'd guess that a simplified approach, dynamically constructing the above query from a calling framework such as Python or C++, would make sense for most applications (in order to minimize work done by the database server).
Let me know if I should elaborate on those possibilities.