Geocoder (Ruby) is a geocoding library for Ruby. Geocoding helps improve web pages by presenting location-relevant information to users. When used with Rails, Geocoder adds geocoding functions such as finding coordinates with street addresses or vice versa besides calculating distances for ActiveRecord objects. Because its function does not rely on proprietary database functions, finding different geocode objects in an area works out of the box for databases like MySQL, PostgreSQL, and SQLite.
Video Geocoder (Ruby)
Compatibility
Geocoder has been fully tested with Ruby 1.8.7, 1.9.2, and JRuby 1.5.3.
Geocoder is compatible with Rails 3, but there is only limited functionality with Rails 2.
Maps Geocoder (Ruby)
Installation
Prerequisites for installing Geocoder are Ruby and RubyGems.
Geocoder gems can be installed with the following command:
gem install geocoder
Or, if you use Bundler for Rails, you can add this to your Gemfile:
gem 'geocoder'
and run in the command prompt:
bundle install
It can be used as a plugin with rails as well:
Configuration
To use Geocoder with objects, the project should be set as follows:
Required Attribute
ActiveRecord
To use Geocoding with an ActiveRecord object, they must have two additional attributes, latitude and longitude coordinates. When stored in a table, they should be called latitude and longitude but they can be changed as described below. When using reverse geocoding (translating the coordinates of a user's location to a physical address), the model must implement the method that returns the address. Address may be a single attribute; However, this can also be a method that returns strings that are strung from different attributes like city, state, and country.
Mongoid
When using Mongoid, the model only needs to add the address, latitude and longitude as the field. The model should also include Geocoder :: Model :: Mongoid before calling the geocoded_by: method.
Model Behavior
In the rail model, Geocoder should be told which method returns the full object address:
For reverse geocoding, Geocoder should know which method returns the latitude and longitude coordinates. If: the address option is not provided, it retrieves the address automatically in the address attribute. Else, it takes the address to the location attribute like the example given below.
Forward and Reverse Geocoding on the same model is possible.
geocoded_by: address reverse_geocoded_by: latitude ,: longitude after_validation: geocode ,: reverse_geocode
To use a different name for the latitude and longitude in the model, the following changes can be made when applying geocoded_by:
Additionally, the address method can return any string that will be used to search Google Maps. One of the following examples will work:
- "123 Sample St, New York, NY"
- "Eiffel Tower, Paris, FR"
- "Raleigh, NC, USA"
Service
By default, Geocoder uses Google's geocoding API to extract addresses and coordinates. Currently, this address geocoding service is supported:
- Google:
: google
- Yahoo:
: yahoo
- Geocoder.ca:
: geocoder_ca
(US & Canada only)
Example
Here are some examples to demonstrate Geocoder functions:
Hotel.near ("Raleigh, North Carolina")
Find hotels near Raleigh.
@ restaurant.distance_to ("Empire State Building")
Find the distance from @restaurant to Empire State Building.
Apps
- Developers can also use Geocoder to convert a user's IP Address to their city location. By doing such a conversion, you may be able to offer user content that is relevant to its current location without requiring prior request.
- This can be used for geo-spatial analysis to recognize patterns in information. This is very useful in data mining applications.
- Web-based GIS tools like Google Maps, Yahoo Maps, etc. include geocoding function.
References
Source of the article : Wikipedia