按纬度和经度查找最近的地点 [英] Find nearest places by latitude and longitude

查看:154
本文介绍了按纬度和经度查找最近的地点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个网站存储一些地方的经度和纬度,我想显示一个特定地点的最近地点(可以说,方圆10公里)。

如何在我的MySQL表上搜索这些地方?

解决方案

几年前,我回答了这样一个问题, Google Answers 以及最近在 Uclue 。我应该注意到,首选的技术惯例是代表西方的经度作为负值,但有时并未观察到。除非你计算的是与Prime Meridian相反的子午线(经线环绕),否则它不会产生太大的变化,但是要注意你的数据库和与其他地理信息库的一致性。



添加:如上面链接的Uclue问题所述,目标是计算两对值,MINLATITUDE / MAXLATITUDE和MINLONGITUDE / MAXLONGITUDE,以便所有点给定位置的给定距离包含在SQL查询的结果中:

  SELECT * FROM位置
WHERE在MINLATITUDE和MAXLATITUDE之间的纬度)
和(经度在MINLONGITUDE和MAXLONGITUDE之间)

5.0我们可以在方便的存储过程中将MIN / MAX对的计算与SELECT一起打包。我们甚至可以用光标运行这种查询的结果,并且如果需要这样的精确度,则检查确切的大圆距离。然而,我猜想一个简化的方法,从一个调用框架(如Python或C ++)动态构建上述查询对于大多数应用程序(为了尽量减少数据库服务器完成的工作)是有意义的。



让我知道是否应该详细说明这些可能性。

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?

解决方案

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:

SELECT * FROM Locations  
    WHERE (latitude  between MINLATITUDE  and MAXLATITUDE )  
    and   (longitude between MINLONGITUDE and MAXLONGITUDE)  

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.

这篇关于按纬度和经度查找最近的地点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆