Search code examples
ruby-on-rails-3geolocationgeocodingdistancegeokit

Distance and range calculation in Geokit/Geocoder


I have the following issue.

Consider 2 models with attributes:

Place
- lat
- lng
- range

User
- lat
- lng

Places have location and some range defined (think about it as area of shipping). User has his own location.

I use Geocoder for geolocation (I could use Geokit as well). I can calculate distance to object (something like object.distance_to([lat,lng])

Now, I'd like to select all places that ranges include my location. At the higher level: I would like to know, which places provide shipping for my location.

It's easier when I'm looking for collection of objects in given range. However I need something "reversed", something from another side.

What method/scope should I write? Maybe Geocoder/Geokit have somthing already for me?


Solution

  • Finally I haven't found anything in gems but I wrote the following method

    class User < ActiveRecord::Base
      #...  
      def in_reach?(place)
        place.distance_to([lat,lng]) <= place.range
      end 
    end