Module: Geocoder::Orm::Base

Included in:
ActiveRecord
Defined in:
lib/geocoder/orms/base.rb

Instance Method Summary collapse

Instance Method Details

#distance_to(lat, lon, units = :mi) ⇒ Object Also known as: distance_from

Calculate the distance from the object to an arbitrary point. Takes two floats (latitude, longitude) and a symbol specifying the units to be used (:mi or :km; default is :mi).



24
25
26
27
28
# File 'lib/geocoder/orms/base.rb', line 24

def distance_to(lat, lon, units = :mi)
  return nil unless geocoded?
  mylat,mylon = to_coordinates
  Geocoder::Calculations.distance_between(mylat, mylon, lat, lon, :units => units)
end

#geocodeObject

Look up coordinates and assign to latitude and longitude attributes (or other as specified in geocoded_by). Returns coordinates (array).



50
51
52
# File 'lib/geocoder/orms/base.rb', line 50

def geocode
  fail
end

#geocoded?Boolean

Is this object geocoded? (Does it have latitude and longitude?)

Returns:

  • (Boolean)


8
9
10
# File 'lib/geocoder/orms/base.rb', line 8

def geocoded?
  to_coordinates.compact.size > 0
end

#nearbys(radius = 20, options = {}) ⇒ Object

Get nearby geocoded objects. Takes the same options hash as the near class method (scope).



36
37
38
39
40
41
42
43
44
# File 'lib/geocoder/orms/base.rb', line 36

def nearbys(radius = 20, options = {})
  return [] unless geocoded?
  if options.is_a?(Symbol)
    options = {:units => options}
    warn "DEPRECATION WARNING: The units argument to the nearbys method has been replaced with an options hash (same options hash as the near scope). You should instead call: obj.nearbys(#{radius}, :units => #{options[:units]}). The old syntax will not be supported in Geocoder v1.0."
  end
  options.merge!(:exclude => self)
  self.class.near(to_coordinates, radius, options)
end

#reverse_geocodeObject

Look up address and assign to address attribute (or other as specified in reverse_geocoded_by). Returns address (string).



58
59
60
# File 'lib/geocoder/orms/base.rb', line 58

def reverse_geocode
  fail
end

#to_coordinatesObject

Coordinates [lat,lon] of the object.



15
16
17
# File 'lib/geocoder/orms/base.rb', line 15

def to_coordinates
  [:latitude, :longitude].map{ |i| send self.class.geocoder_options[i] }
end