Geocoder is a Rails Gem which helps you to manipulate location in your Rails application. It is written by Alex Reisner. Thanks for his work and you can find out more details at Github – alexreisner / geocoder.
Let’s try it now.
1. Add the Geocoder Gem in your application by adding the following line in the Gemfile
- gem ‘geocoder’
2. Run bundle install to install the newly added Gem
3. Start the Rails console and you can try the Geocoder functions
- Geocoder.coordinates(“Hong Kong”)
- Geocoder.search(“Hong Kong”)
The above coordinates are provided by Google which is the default Geocoding API. You can switch to other services by editing the config/initializers/geocoder.rb.
Geocoder::Configuration.lookup = :google #Geocoder::Configuration.lookup = :yahoo #Geocoder::Configuration.lookup = :geocoder_ca
You can also integrate the Geocoder with models. I will spend some times on it later.
Reference: Github – alexreisner / geocoder