如何使用 Ruby Geocoder/Mongoid 查询某个点附近的对象?

发布于 2024-11-19 13:46:36 字数 2386 浏览 0 评论 0原文

我正在尝试做这样的事情:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吹梦到西洲 2024-11-26 13:46:36

您的地理空间查询的 mongoid 查询不正确,

而是

@places = Place.near(loc.coordinates)

您必须使用此

@places = Place.near(:loc => loc.coordinates).

:loc 是在 mongo 文档中获取位置坐标的列/字段名称。

编辑:

我还忘记了一件事

谷歌地理编码器以[lat,lng]格式返回坐标,但mongo要求它们以[lng,lat]格式 格式。您最好反转查询中的坐标。

Your mongoid query for Geospatial query is incorrect

instead this

@places = Place.near(loc.coordinates)

You have to use this

@places = Place.near(:loc => loc.coordinates).

: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.

痴情 2024-11-26 13:46:36

将 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 to loc.to_coordinates otherwise you'll end up in Antarctica when you're looking for New York.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文