If you are building a property rental application or a transportation startup and using maps heavily, then the Geocoder gem would be your best friend.
What is the Geocoder gem for?
Geocoder gem is a geocoding solution for Ruby. With Rails it adds geocoding (by street or IP address), reverse geocoding (finding street address based on given coordinates), and distance queries.
What is geocoding?
Geocoding is the process of transforming a description of a location—such as a pair of coordinates, an address, or a name of a place to a location on the earth’s surface.
How do you install Geocoder gem on your rails application?
Install Geocoder like any other Ruby gem:
How do you use Geocoder in ActiveRecord?
The model that needs Geocoding must have the two attributes for storing latitude and longitude. Generate a migration to add the attributes to your target model:
To gecode your model, you must provide a method that provides an address. Then let geocoder the which method returns the the address.
Hooking up Geocoder with Google
Geocoder can be hooked up to different street and IP address geocoding services. I will discuss how you can hook your geocoder gem with google. Although the default lookup for street address uses google, you can increase quota by adding your api key (and adding your Credit Card to your developer account in Google).
To create a Rails initializer with an example configuration, use this command:
rails generate geocoder:config
Your config should look like this:
Geocoder.configure( # Geocoding options timeout: 100000, # geocoding service timeout (secs) lookup: :google, # name of geocoding service (symbol) # language: :en, # ISO-639 language code use_https: true, # use HTTPS for lookup requests? (if supported) # http_proxy: nil, # HTTP proxy server (user:pass@host:port) # https_proxy: nil, # HTTPS proxy server (user:pass@host:port) api_key: 'DUMMY_API_KEY', # API key for geocoding service # cache: nil, # cache object (must respond to #, #=, and #keys) # cache_prefix: 'geocoder:', # prefix (string) to use for all cache keys # Exceptions that should not be rescued by default # (if you want to implement custom error handling); # supports SocketError and Timeout::Error always_raise: :all, # Calculation options units: :mi, # :km for kilometers or :mi for miles # distances: :linear # :spherical or :linear )
Make sure you add your api key.
Where can you get your google api key?
Access your google developer console.
For more information, visit the official geocoder gem website: http://www.rubygeocoder.com
Good luck with using Geocoder gem on your startup!