diff --git a/README.rdoc b/README.rdoc index 74d4d5bc5ee0c010898df2579851d6079aef25e0..3ae8dec628a7cb3a31d8b6e763823748a93d74fb 100644 --- a/README.rdoc +++ b/README.rdoc @@ -41,11 +41,9 @@ Find distance between object and a point: obj.distance_to(40.71432, -100.23487) # in miles obj.distance_to(40.71432, -100.23487, :km) # in kilometers -If you're using a MySQL database and you need to do a search for objects -within a given distance from a point, this method will generate a query for you: - - Geocoder.nearby_mysql_query('cities', 40.71432, -100.23487, 50) +Find objects close to a point (Venue is a geocoded model): + Venue.near('Omaha, NE, US') Please see the code for more methods and detailed information about arguments. diff --git a/lib/geocoder.rb b/lib/geocoder.rb index 6bc5ef5225d92d3cd4057da77c0428175481140e..bbcb03af6040f5a2321b2697a03faf343bb695a0 100644 --- a/lib/geocoder.rb +++ b/lib/geocoder.rb @@ -51,14 +51,20 @@ module Geocoder def near(location, radius = 100, options = {}) latitude, longitude = Geocoder.fetch_coordinates(location) return [] unless (latitude and longitude) - # don't pass :table_name option to nearby_mysql_query - table_name = options[:table_name] || self.to_s.tableize - options.delete :table_name - query = Geocoder.nearby_mysql_query(table_name, - latitude, longitude, radius.to_i, options) + query = nearby_mysql_query(latitude, longitude, radius.to_i, options) find_by_sql(query) end + ## + # Generate a MySQL query to find all records within a radius (in miles) + # of a point. + # + def nearby_mysql_query(latitude, longitude, radius = 20, options = {}) + table = options[:table_name] || self.to_s.tableize + options.delete :table_name # don't pass to nearby_mysql_query + Geocoder.nearby_mysql_query(table, latitude, longitude, radius, options) + end + ## # Get the name of the method that returns the search string. #