Created
August 12, 2013 22:34
-
-
Save petehuang/6215973 to your computer and use it in GitHub Desktop.
How we made our geocoding faster. This will also avoid the daily rate limit for Google Maps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This goes as a before_filter on your controller. | |
#Requires 'geocoder' gem | |
def check_zipcode | |
if params[:zipcode].nil? | |
return | |
end | |
if !Zipcode.where('zip = ?', params[:zipcode]).empty? | |
zip = Zipcode.where('zip = ?', params[:zipcode]).first | |
return [zip.lat, zip.lng] | |
else | |
search = Geocoder.search(params[:zipcode]).first.geometry["location"] | |
lat = search["lat"] | |
lng = search["lng"] | |
zip = Zipcode.new(zip: params[:zipcode], lat: lat, lng: lng) | |
zip.save | |
return [lat, lng] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment