/geo-calculator

Use Ruby Geocoder Calculation methods in a model without all the cruft.

Primary LanguageRubyMIT LicenseMIT

GeoCalculator

GeoCalculator is an extraction of the calculation methods from the Ruby Geocoder gem. Its purpose is to allow the geocoding calculation methods for models that already have latitude and longitude (obtained from the browser or an iphone app) to use distance calculations to find nearby model instances. It was extracted for use in Heat.

Installation

Add this line to your application's Gemfile:

gem 'geo-calculator'

And then execute:

$ bundle

Or install it yourself as:

$ gem install geo-calculator

Usage

Add the line include Geocoder::Store::ActiveRecord into your model like so:

# app/models/location.rb
class Location < ActiveRecord::Base
  include Geocoder::Store::ActiveRecord

  def self.geocoder_options
    {
      latitude: 'latitude',
      longitude: 'longitude',
      units: :mi
    }
  end
end

make sure that your model has a latitude and longitude column.

Make sure to define the geocoder_options method, and specify the names of your latitude / longitude columns plus the units you want to use.

This will give your model a bunch of calculation methods, like .near and .within_bounding_box. See them all here

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request