Module: Geocoder::Store::Base

Included in:
ActiveRecord, MongoMapper, Mongoid
Defined in:
lib/geocoder/stores/base.rb

Instance Method Summary collapse

Instance Method Details

#bearing_from(point, options = {}) ⇒ Object

Calculate the bearing from another point to the object. See Geocoder::Calculations.distance_between for ways of specifying the point.



49
50
51
52
53
# File 'lib/geocoder/stores/base.rb', line 49

def bearing_from(point, options = {})
  return nil unless geocoded?
  Geocoder::Calculations.bearing_between(
    point, to_coordinates, options)
end

#bearing_to(point, options = {}) ⇒ Object

Calculate the bearing from the object to another point. See Geocoder::Calculations.distance_between for ways of specifying the point.



38
39
40
41
42
# File 'lib/geocoder/stores/base.rb', line 38

def bearing_to(point, options = {})
  return nil unless geocoded?
  Geocoder::Calculations.bearing_between(
    to_coordinates, point, options)
end

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

Calculate the distance from the object to an arbitrary point. See Geocoder::Calculations.distance_between for ways of specifying the point. Also takes a symbol specifying the units (:mi or :km; default is :mi).



25
26
27
28
29
# File 'lib/geocoder/stores/base.rb', line 25

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

#geocodeObject

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



69
70
71
# File 'lib/geocoder/stores/base.rb', line 69

def geocode
  fail
end

#geocoded?Boolean

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

Returns:

  • (Boolean)


8
9
10
# File 'lib/geocoder/stores/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).



59
60
61
62
63
# File 'lib/geocoder/stores/base.rb', line 59

def nearbys(radius = 20, options = {})
  return [] unless geocoded?
  options.merge!(:exclude => self)
  self.class.near(self, radius, options)
end

#reverse_geocodeObject

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



77
78
79
# File 'lib/geocoder/stores/base.rb', line 77

def reverse_geocode
  fail
end

#to_coordinatesObject

Coordinates [lat,lon] of the object.



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

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