Module: ActsAsGeocodable::Model::InstanceMethods

Defined in:
lib/acts_as_geocodable.rb

Instance Method Summary collapse

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.



202
203
204
205
206
207
208
# File 'lib/acts_as_geocodable.rb', line 202

def distance_to(destination, options = {})
  units = options[:units] || acts_as_geocodable_options[:units]
  formula = options[:formula] || :haversine

  geocode = self.class.location_to_geocode(destination)
  self.geocode.distance_to(geocode, units, formula)
end

#geocodeObject

Get the geocode for this model



177
178
179
# File 'lib/acts_as_geocodable.rb', line 177

def geocode
  geocoding.geocode if geocoding
end

#to_locationObject

Create a Graticule::Location



182
183
184
185
186
187
188
# File 'lib/acts_as_geocodable.rb', line 182

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