Mongoid Model.near 方法的最大距离
我已经使用地理附近查询有一段时间了,但我似乎不知道如何将结果限制在一定的半径内。例如,如何在此查询中将搜索范围限制在 20 英里以内?
Place.near(:coordinates => location.reverse)
# must reverse the resulting coordinates array because mongo stores them backwards [lng,lat]
I've been using the geo near queries for a while now, but I can't seem to figure out how to limit the results to within a certain radius. For example, how do I limit the search to within 20 miles in this query?
Place.near(:coordinates => location.reverse)
# must reverse the resulting coordinates array because mongo stores them backwards [lng,lat]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
南北纬度差弧度的长度,在任何纬度约为60海里、111公里或69法定英里;您可以在维基百科或 mongo 地理空间页面 地球是圆的,但地图是平的。
使用英里或公里时分别将距离除以 69 或 111,所以现在您可以这样查询
The length of an arcdegree of north-south latitude difference, is about 60 nautical miles, 111 kilometres or 69 statute miles at any latitude; You can read more about here in wikipedia or in mongo geospatial page The Earth is Round but Maps are Flat.
Divide distance by 69 or 111 when using mile or km respectively, so now you can query it like this
MongoDB 支持 maxDistance 属性(MongoDB 查询)
mongoid_geo 扩展有一个within_box 和 within_center 方法。
MongoDB supports a maxDistance attribute (MongoDB Querying)
And the mongoid_geo extension has a within_box and within_center method.
控制器 :
controller :
试试这个...
Mongo 要求的 $maxDistance 是以弧度为单位的,因此需要将地球表面的距离转换为纬度/经度角度。
https://en.wikipedia.org/wiki/Arc_(geometry)
http://docs.mongodb.org/manual/tutorial/calculate-distances-using-spherical-geometry-with-2d-geospatial-indexes/
角度(弧度)= distance_at_surface / radius_of_earth
角度 = 10km / 6371km
角度 = 10sm / 3960sm
Try this...
The $maxDistance required by Mongo is in radians, so you need to convert the distance at the surface of the earth into a latitude/longitude angle.
https://en.wikipedia.org/wiki/Arc_(geometry)
http://docs.mongodb.org/manual/tutorial/calculate-distances-using-spherical-geometry-with-2d-geospatial-indexes/
angle (radians) = distance_at_surface / radius_of_earth
angle = 10km / 6371km
angle = 10sm / 3960sm