确定范围内的经度和纬度
我的数据库中有位置。位置具有纬度和经度属性(取自谷歌地图,例如:48.809591)。 是否有任何查询可以帮助我检索另一个位置范围内的位置?
例子: 我的位置 A 的纬度 = 48.809591,经度 = 2.124009,并且想要检索数据库中距离位置 A 5 英里以内的所有位置对象
。 A.latitude + 5 英里和 location.latitude > A.latitude - 5 英里且 location.longitude < A.经度 + 5 英里且 location.经度 > A.longitude - 5 英里,然后借助 http://www.movable-type.co.uk/scripts/latlong.html
有什么想法吗?
I have locations in my database. A location has the attributes latitude and longitude (taken from google maps, example: 48.809591).
Is there any query that could help me retrieve the locations within a range of another location?
Example:
I have the location A with latitude = 48.809591, and longitude = 2.124009 and want to retrieve all location objects in my database that are within 5 miles of location A
My first thought was to retrieve the locations in a square where location.latitude < A.latitude + 5 miles and location.latitude > A.latitude - 5 miles and location.longitude < A.longitude + 5 miles and location.longitude > A.longitude - 5 miles, and then remove the irrelevant locations from the returned array with the help of something like http://www.movable-type.co.uk/scripts/latlong.html
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 MySQL 作为 DBMS1,您可能有兴趣查看以下演示文稿:
作者介绍了如何使用 MySQL 中的半正弦公式可按邻近度对空间数据进行排序,并将结果限制在定义的半径内。更重要的是,他还描述了如何使用纬度和经度列上的传统索引来避免此类查询的全表扫描。
1 即使您不是,这仍然很有趣且适用。
2 该演示文稿还有一个 pdf 版本。
Just in case you're using MySQL as your DBMS1, you may be interested in checking out the following presentation:
The author describes how you can use the Haversine Formula in MySQL to order spatial data by proximity and limit the results to a defined radius. More importantly, he also describes how to avoid a full table scan for such queries, using traditional indexes on the latitude and longitude columns.
1 Even if you aren't, this is still interesting and applicable.
2 There is also a pdf version of the presentation.
我认为,您想要的计算称为大圆距离:
http://en.wikipedia。 org/wiki/Great-circle_distance
The calculation you want, i think, is called the great circle distance:
http://en.wikipedia.org/wiki/Great-circle_distance
你需要一个距离函数。
对于SQL Server,它看起来像这样(注意距离以公里为单位),
You would need a distance function.
For SQL Server it would look something like this (note that distance is in kilometers),