mongoid/mongoid-geospatial

Nearby with range

lairtonmendes opened this issue · 1 comments

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

@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