module Geocoder::Orm::Base
def distance_to(lat, lon, units = :mi)
units to be used (:mi or :km; default is :mi).
Takes two floats (latitude, longitude) and a symbol specifying the
Calculate the distance from the object to an arbitrary point.
#
def distance_to(lat, lon, units = :mi) return nil unless geocoded? mylat,mylon = read_coordinates Geocoder::Calculations.distance_between(mylat, mylon, lat, lon, :units => units) end
def do_lookup(reverse = false)
Geocoder::Result object with the geocoding results).
given two-arguments: the object being geocoded and a
block (given to geocoded_by or reverse_geocoded_by). The block is
geocoded_by or reverse_geocoded_by) and handle the result with the
Look up geographic data based on object attributes (configured in
#
def do_lookup(reverse = false) options = self.class.geocoder_options if reverse and options[:reverse_geocode] args = [:latitude, :longitude] elsif !reverse and options[:geocode] args = [:user_address] else return end args.map!{ |a| send(options[a]) } if result = Geocoder.search(*args) # execute custom block, if specified in configuration block_key = reverse ? :reverse_block : :geocode_block if custom_block = options[block_key] custom_block.call(self, result) # else execute block passed directly to this method, # which generally performs the "auto-assigns" elsif block_given? yield(self, result) end end end
def geocode
(or other as specified in +geocoded_by+). Returns coordinates (array).
Look up coordinates and assign to +latitude+ and +longitude+ attributes
#
def geocode fail end
def geocoded?
Is this object geocoded? (Does it have latitude and longitude?)
#
def geocoded? read_coordinates.compact.size > 0 end
def nearbys(radius = 20, units = :mi)
representing the units of the ratius (:mi or :km; default is :mi).
Get nearby geocoded objects. Takes a radius (integer) and a symbol
#
def nearbys(radius = 20, units = :mi) return [] unless geocoded? options = {:exclude => self, :units => units} self.class.near(read_coordinates, radius, options) end
def read_coordinates
Looks at user config to determine attributes.
Read the coordinates [lat,lon] of the object.
#
def read_coordinates [:latitude, :longitude].map{ |i| send self.class.geocoder_options[i] } end
def reverse_geocode
in +reverse_geocoded_by+). Returns address (string).
Look up address and assign to +address+ attribute (or other as specified
#
def reverse_geocode fail end