@@ -53,25 +53,25 @@ def find_near(location, radius = 20, options = {})
5353 #
5454 def find_near_options ( latitude , longitude , radius = 20 , options = { } )
5555
56- # Set defaults/clean up arguments.
56+ # set defaults/clean up arguments
5757 options [ :order ] ||= 'distance ASC'
5858 radius = radius . to_i
5959
60- # Constrain search to a (radius x radius) square.
60+ # constrain search to a (radius x radius) square
6161 factor = ( Math ::cos ( latitude * Math ::PI / 180.0 ) * 69.0 ) . abs
6262 lon_lo = longitude - ( radius / factor ) ;
6363 lon_hi = longitude + ( radius / factor ) ;
6464 lat_lo = latitude - ( radius / 69.0 ) ;
6565 lat_hi = latitude + ( radius / 69.0 ) ;
6666
67- # Build limit clause.
67+ # build limit clause
6868 limit = nil
6969 if options [ :limit ] or options [ :offset ]
7070 options [ :offset ] ||= 0
7171 limit = "#{ options [ :offset ] } ,#{ options [ :limit ] } "
7272 end
7373
74- # Generate hash.
74+ # generate hash
7575 lat_attr = geocoder_options [ :latitude ]
7676 lon_attr = geocoder_options [ :longitude ]
7777 {
@@ -156,14 +156,14 @@ def fetch_coordinates!
156156 def self . fetch_coordinates ( query )
157157 return nil unless doc = self . search ( query )
158158
159- # Make sure search found a result.
159+ # make sure search found a result
160160 e = doc . elements [ 'kml/Response/Status/code' ]
161161 return nil unless ( e and e . text == "200" )
162162
163- # Isolate the relevant part of the result.
163+ # isolate the relevant part of the result
164164 place = doc . elements [ 'kml/Response/Placemark' ]
165165
166- # If there are multiple results, blindly use the first.
166+ # if there are multiple results, blindly use the first
167167 coords = place . elements [ 'Point/coordinates' ] . text
168168 coords . split ( ',' ) [ 0 ...2 ] . reverse . map { |i | i . to_f }
169169 end
@@ -175,20 +175,23 @@ def self.fetch_coordinates(query)
175175 # +units+ :: <tt>:mi</tt> for miles (default), <tt>:km</tt> for kilometers
176176 #
177177 def self . distance_between ( lat1 , lon1 , lat2 , lon2 , options = { } )
178+
178179 # set default options
179180 options [ :units ] ||= :mi
180- # define available units
181+
182+ # define conversion factors
181183 units = { :mi => 3956 , :km => 6371 }
182184
183185 # convert degrees to radians
184186 lat1 = to_radians ( lat1 )
185187 lon1 = to_radians ( lon1 )
186188 lat2 = to_radians ( lat2 )
187189 lon2 = to_radians ( lon2 )
190+
188191 # compute distances
189192 dlat = ( lat1 - lat2 ) . abs
190193 dlon = ( lon1 - lon2 ) . abs
191-
194+
192195 a = ( Math . sin ( dlat / 2 ) ) **2 + Math . cos ( lat1 ) *
193196 ( Math . sin ( dlon / 2 ) ) **2 * Math . cos ( lat2 )
194197 c = 2 * Math . atan2 ( Math . sqrt ( a ) , Math . sqrt ( 1 -a ) )
@@ -230,3 +233,22 @@ def self.search(query)
230233 REXML ::Document . new ( doc )
231234 end
232235end
236+
237+ ##
238+ # Add geocoded_by method to ActiveRecord::Base so Geocoder is accessible.
239+ #
240+ ActiveRecord ::Base . class_eval do
241+
242+ ##
243+ # Set attribute names and include the Geocoder module.
244+ #
245+ def self . geocoded_by ( method_name = :location , options = { } )
246+ class_inheritable_reader :geocoder_options
247+ write_inheritable_attribute :geocoder_options , {
248+ :method_name => method_name ,
249+ :latitude => options [ :latitude ] || :latitude ,
250+ :longitude => options [ :longitude ] || :longitude
251+ }
252+ include Geocoder
253+ end
254+ end
0 commit comments