Module: Geocoder::ClassMethods
- Defined in:
- lib/geocoder.rb
Overview
Methods which will be class methods of the including class.
Instance Method Summary collapse
-
#_get_coordinates(object) ⇒ Object
Get the coordinates [lat,lon] of an object.
-
#find_near(location, radius = 20, options = {}) ⇒ Object
DEPRECATED: Please use the
nearmethod/named scope instead. -
#near_scope_options(latitude, longitude, radius = 20, options = {}) ⇒ Object
Get options hash suitable for passing to ActiveRecord.find to get records within a radius (in miles) of the given point.
Instance Method Details
#_get_coordinates(object) ⇒ Object
Get the coordinates [lat,lon] of an object. This is not great but it seems cleaner than polluting the object method namespace.
110 111 112 113 |
# File 'lib/geocoder.rb', line 110 def _get_coordinates(object) [object.send([:latitude]), object.send([:longitude])] end |
#find_near(location, radius = 20, options = {}) ⇒ Object
DEPRECATED: Please use the near method/named scope instead.
48 49 50 51 52 |
# File 'lib/geocoder.rb', line 48 def find_near(location, radius = 20, = {}) warn "Geocoder deprecation warning: the 'find_near' class method is " + "deprecated, please use the 'near' method, which is a named scope." near(location, radius, ) end |
#near_scope_options(latitude, longitude, radius = 20, options = {}) ⇒ Object
Get options hash suitable for passing to ActiveRecord.find to get records within a radius (in miles) of the given point. Taken from excellent tutorial at: www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL
Options hash may include:
order-
column(s) for ORDER BY SQL clause
limit-
number of records to return (for LIMIT SQL clause)
offset-
number of records to skip (for LIMIT SQL clause)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/geocoder.rb', line 66 def (latitude, longitude, radius = 20, = {}) # set defaults/clean up arguments [:order] ||= 'distance ASC' radius = radius.to_i # constrain search to a (radius x radius) square factor = (Math::cos(latitude * Math::PI / 180.0) * 69.0).abs lon_lo = longitude - (radius / factor); lon_hi = longitude + (radius / factor); lat_lo = latitude - (radius / 69.0); lat_hi = latitude + (radius / 69.0); # build limit clause limit = nil if [:limit] or [:offset] [:offset] ||= 0 limit = "#{options[:offset]},#{options[:limit]}" end # generate hash lat_attr = [:latitude] lon_attr = [:longitude] { :select => "*, 3956 * 2 * ASIN(SQRT(" + "POWER(SIN((#{latitude} - #{lat_attr}) * " + "PI() / 180 / 2), 2) + COS(#{latitude} * PI()/180) * " + "COS(#{lat_attr} * PI() / 180) * " + "POWER(SIN((#{longitude} - #{lon_attr}) * " + "PI() / 180 / 2), 2) )) as distance", :conditions => [ "#{lat_attr} BETWEEN ? AND ? AND " + "#{lon_attr} BETWEEN ? AND ?", lat_lo, lat_hi, lon_lo, lon_hi], :having => "distance <= #{radius}", :order => [:order], :limit => limit } end |