如何使用 Ruby Geocoder/Mongoid 查询某个点附近的对象?
我正在尝试做这样的事情:
if params[:q]
loc = Geocoder.search(params[:q])[0]
logger.info loc.coordinates
@places = Place.near(loc.coordinates).paginate(:per_page => 20, :page => params[:page])
else
...
它设法正确地对查询字符串进行地理编码,但不允许我对其进行查询。 当您尝试查找特定坐标附近的对象时,查询使用地理编码器的模型会引发以下错误:
Mongo::OperationFailure
geo values have to be numbers
应用程序正在运行 Mongoid,且 Ruby Geocoder 用于地理编码的 gem。
更新以下是地理编码结果的输出:
<Geocoder::Result::Google:0x102fe38c0 @data={"address_components"=>[{"long_name"=>"627", "types"=>["street_number"], "short_name"=>"627"}, {"long_name"=>"3rd Ave", "types"=>["route"], "short_name"=>"3rd Ave"}, {"long_name"=>"Manhattan", "types"=>["sublocality", "political"], "short_name"=>"Manhattan"}, {"long_name"=>"New York", "types"=>["locality", "political"], "short_name"=>"New York"}, {"long_name"=>"New York", "types"=>["administrative_area_level_2", "political"], "short_name"=>"New York"}, {"long_name"=>"New York", "types"=>["administrative_area_level_1", "political"], "short_name"=>"NY"}, {"long_name"=>"United States", "types"=>["country", "political"], "short_name"=>"US"}, {"long_name"=>"10017", "types"=>["postal_code"], "short_name"=>"10017"}], "types"=>["street_address"], "partial_match"=>true, "geometry"=>{"location"=>{"lng"=>-73.9750644, "lat"=>40.7498908}, "bounds"=>{"northeast"=>{"lng"=>-73.9750644, "lat"=>40.7498994}, "southwest"=>{"lng"=>-73.9750849, "lat"=>40.7498908}}, "location_type"=>"RANGE_INTERPOLATED", "viewport"=>{"northeast"=>{"lng"=>-73.9719270293198, "lat"=>40.7530427206802}, "southwest"=>{"lng"=>-73.9782222706802, "lat"=>40.7467474793198}}}, "formatted_address"=>"627 3rd Ave, New York, NY 10017, USA"}>
MONGODB development['places'].find({:coordinates=>{"$nearSphere"=>[-73.9750644, 40.7498908], "$maxDistance"=>0.00505209229513324}})
Completed in 504ms
Mongo::OperationFailure (geo values have to be numbers):
app/controllers/places_controller.rb:11:in `index'
app/middleware/flash_session_cookie_middleware.rb:17:in `call'
app/middleware/flash_session_cookie_middleware.rb:17:in `call'
I am trying to do something like this:
if params[:q]
loc = Geocoder.search(params[:q])[0]
logger.info loc.coordinates
@places = Place.near(loc.coordinates).paginate(:per_page => 20, :page => params[:page])
else
...
It manages to geocode the query string correctly, but doesn't let me query against it.
Querying a model that uses geocoder raises the following error when you try to find objects near specific coordinates:
Mongo::OperationFailure
geo values have to be numbers
The app is running Mongoid, with the Ruby Geocoder gem for geocoding.
UPDATE Here is the output of the geocode result:
<Geocoder::Result::Google:0x102fe38c0 @data={"address_components"=>[{"long_name"=>"627", "types"=>["street_number"], "short_name"=>"627"}, {"long_name"=>"3rd Ave", "types"=>["route"], "short_name"=>"3rd Ave"}, {"long_name"=>"Manhattan", "types"=>["sublocality", "political"], "short_name"=>"Manhattan"}, {"long_name"=>"New York", "types"=>["locality", "political"], "short_name"=>"New York"}, {"long_name"=>"New York", "types"=>["administrative_area_level_2", "political"], "short_name"=>"New York"}, {"long_name"=>"New York", "types"=>["administrative_area_level_1", "political"], "short_name"=>"NY"}, {"long_name"=>"United States", "types"=>["country", "political"], "short_name"=>"US"}, {"long_name"=>"10017", "types"=>["postal_code"], "short_name"=>"10017"}], "types"=>["street_address"], "partial_match"=>true, "geometry"=>{"location"=>{"lng"=>-73.9750644, "lat"=>40.7498908}, "bounds"=>{"northeast"=>{"lng"=>-73.9750644, "lat"=>40.7498994}, "southwest"=>{"lng"=>-73.9750849, "lat"=>40.7498908}}, "location_type"=>"RANGE_INTERPOLATED", "viewport"=>{"northeast"=>{"lng"=>-73.9719270293198, "lat"=>40.7530427206802}, "southwest"=>{"lng"=>-73.9782222706802, "lat"=>40.7467474793198}}}, "formatted_address"=>"627 3rd Ave, New York, NY 10017, USA"}>
MONGODB development['places'].find({:coordinates=>{"$nearSphere"=>[-73.9750644, 40.7498908], "$maxDistance"=>0.00505209229513324}})
Completed in 504ms
Mongo::OperationFailure (geo values have to be numbers):
app/controllers/places_controller.rb:11:in `index'
app/middleware/flash_session_cookie_middleware.rb:17:in `call'
app/middleware/flash_session_cookie_middleware.rb:17:in `call'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的地理空间查询的 mongoid 查询不正确,
而是
您必须使用此
:loc 是在 mongo 文档中获取位置坐标的列/字段名称。
编辑:
我还忘记了一件事
谷歌地理编码器以[lat,lng]格式返回坐标,但mongo要求它们以[lng,lat]格式 格式。您最好反转查询中的坐标。
Your mongoid query for Geospatial query is incorrect
instead this
You have to use this
:loc is the column/field name got the location co-ordinates in mongo document.
Edit:
And i forgot one more thing
Google geocoder returns the co-ordinates in [lat,lng] format, but mongo requires them in [lng,lat] format. You better reverse the co-ordinate in the query.
将 Mongoid 与 Geocoder 结合使用时,您需要注意一些事项。确保您已在 Place 模型中包含 Mongoid 特定行。
另外,请将
loc.coordinates
切换为loc.to_coordinates
,否则当您寻找纽约时,您最终会到达南极洲。There's a couple of things you need to be aware of when using Mongoid with Geocoder. Make sure you've included the Mongoid specific lines in your Place model.
Also, switch
loc.coordinates
toloc.to_coordinates
otherwise you'll end up in Antarctica when you're looking for New York.