开发者

How do I query objects near a point with Ruby Geocoder/Mongoid?

I am trying to do something like this:

if开发者_如何学C 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'


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜