mongoid / mongoid-geospatial

A MongoDB/Mongoid Extension with optional GeoRuby/RGeo support.
http://rubygems.org/gems/mongoid-geospatial
MIT License
80 stars 31 forks source link

Nearby with range #72

Open lairtonmendes opened 5 years ago

lairtonmendes commented 5 years ago

Hello dear,

I need make this query below, but I need set the maxDistance por exemplo 1000 meters Bar.nearby(my.location)

In MongoDB documentation

{
   <location field>: {
     $near: {
       $geometry: {
          type: "Point" ,
          coordinates: [ <longitude> , <latitude> ]
       },
       $maxDistance: <distance in meters>,
       $minDistance: <distance in meters>
     }
   }
}

thank you very munch

thiensubs commented 4 years ago

@lairtonmendes Maybe you can apply code bellow:

 lat = params[:lat].to_f
 lng = params[:lng].to_f
 # i'm using distance with kilometer, if you using meter, please convert it.
 radius = params[:distance].to_f / 111.12
 radius_min = params[:distance_min].to_f / 111.12
 aps = Model.where(location: { "$nearSphere": [lng, lat], "$maxDistance": radius,  "$minDistance": radius_min}).to_a