我对使用mongoid的geonear查询感到困惑。似乎我错过了一些东西,当我试图查询给定坐标附近的点时,最大距离为200米,形成我的控制器:
Coord.geo_near([params[:lon].to_i, params[:lat].to_i]).max_distance(200)
对于我提供的每组坐标,我都以相同的顺序获得集合中可用的所有点。
我的模特:
class Coord
include Mongoid::Document
include Mongoid::Geospatial
field :Message, :type => String
field :location, type: Point, spatial: true
end
我已经从mongo控制台创建了2dsphere索引:
db.coords.createIndex( { location : "2dsphere" } )
在max_distance与度数一起工作的情况下,我也试过而不是200米来提供0.2 / 111.2但在这种情况下我没有得到任何结果。
答案 0 :(得分:-1)
使用地球半径
在半径10公里范围内搜索:
Coord.geo_near([params[:lon].to_f, params[:lat].to_f]).max_distance(10.fdiv(6_371))