Skip to content

Instantly share code, notes, and snippets.

@petehuang
Created August 12, 2013 22:34
Show Gist options
  • Save petehuang/6215973 to your computer and use it in GitHub Desktop.
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 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