Module: Geocoder::Orm::Base
- Included in:
- ActiveRecord
- Defined in:
- lib/geocoder/orms/base.rb
Instance Method Summary collapse
-
#distance_to(lat, lon, units = :mi) ⇒ Object
(also: #distance_from)
Calculate the distance from the object to an arbitrary point.
-
#geocode ⇒ Object
Look up coordinates and assign to
latitude
andlongitude
attributes (or other as specified ingeocoded_by
). -
#geocoded? ⇒ Boolean
Is this object geocoded? (Does it have latitude and longitude?).
-
#nearbys(radius = 20, options = {}) ⇒ Object
Get nearby geocoded objects.
-
#reverse_geocode ⇒ Object
Look up address and assign to
address
attribute (or other as specified inreverse_geocoded_by
). -
#to_coordinates ⇒ Object
Coordinates [lat,lon] of the object.
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 |
#geocode ⇒ Object
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?)
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, = {}) return [] unless geocoded? if .is_a?(Symbol) = {:units => } 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 => #{[:units]}). The old syntax will not be supported in Geocoder v1.0." end .merge!(:exclude => self) self.class.near(to_coordinates, radius, ) end |
#reverse_geocode ⇒ Object
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_coordinates ⇒ Object
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.[i] } end |