Module: ActsAsGeocodable::Model
- Defined in:
- lib/acts_as_geocodable.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#distance_to(destination, options = {}) ⇒ Object
Get the distance to the given destination.
-
#geocode ⇒ Object
Get the geocode for this model.
-
#to_location ⇒ Object
Create a Graticule::Location.
Class Method Details
.included(base) ⇒ Object
109 110 111 |
# File 'lib/acts_as_geocodable.rb', line 109 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#distance_to(destination, options = {}) ⇒ Object
Get the distance to the given destination. The destination can be an acts_as_geocodable model, a Geocode, or a string
myhome.distance_to "Chicago, IL"
myhome.distance_to "49423"
myhome.distance_to other_model
Options
-
:units
::miles
or:kilometers
-
:formula
: The formula to use to calculate the distance. This can be any formula supported by Graticule. The default is:haversine
.
208 209 210 211 212 213 214 |
# File 'lib/acts_as_geocodable.rb', line 208 def distance_to(destination, = {}) units = [:units] || [:units] formula = [:formula] || :haversine geocode = self.class.location_to_geocode(destination) self.geocode.distance_to(geocode, units, formula) end |
#geocode ⇒ Object
Get the geocode for this model
183 184 185 |
# File 'lib/acts_as_geocodable.rb', line 183 def geocode geocoding.geocode if geocoding end |
#to_location ⇒ Object
Create a Graticule::Location
188 189 190 191 192 193 194 |
# File 'lib/acts_as_geocodable.rb', line 188 def to_location Graticule::Location.new.tap do |location| [:street, :locality, :region, :postal_code, :country].each do |attr| location.send("#{attr}=", geo_attribute(attr)) end end end |