Skip to content
Snippets Groups Projects
Commit ed04ff41 authored by Alex Reisner's avatar Alex Reisner
Browse files

Add nearby_mysql_query class method to geocoded class.

parent b015a546
No related branches found
No related tags found
No related merge requests found
......@@ -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.
......
......@@ -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.
#
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment