Module: GeoKit::ActsAsMappable

Defined in:
lib/geo_kit/acts_as_mappable.rb

Overview

Contains the class method acts_as_mappable targeted to be mixed into ActiveRecord. When mixed in, augments find services such that they provide distance calculation query services. The find method accepts additional options:

  • :origin - can be

    1. a two-element array of latititude/longitude – :origin=>

    2. a geocodeable string – :origin=>‘100 Spear st, San Francisco, CA’

    3. an object which responds to lat and lng methods, or latitude and longitude methods, or whatever methods you have specified for lng_column_name and lat_column_name

Other finder methods are provided for specific queries. These are:

  • find_within (alias: find_inside)

  • find_beyond (alias: find_outside)

  • find_closest (alias: find_nearest)

  • find_farthest

Counter methods are available and work similarly to finders.

If raw SQL is desired, the distance_sql method can be used to obtain SQL appropriate to use in a find_by_sql call.

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Mix below class methods into ActiveRecord.



25
26
27
# File 'lib/geo_kit/acts_as_mappable.rb', line 25

def self.included(base) # :nodoc:
  base.extend ClassMethods
end

Instance Method Details

#auto_geocode_addressObject

this is the callback for auto_geocoding



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/geo_kit/acts_as_mappable.rb', line 79

def auto_geocode_address
  address=self.send(auto_geocode_field)
  geo=GeoKit::Geocoders::MultiGeocoder.geocode(address)
  
  if geo.success
    self.send("#{lat_column_name}=", geo.lat)
    self.send("#{lng_column_name}=", geo.lng)
  else
    errors.add(auto_geocode_field, auto_geocode_error_message) 
  end
  
  geo.success
end