Jump to content

Geocoder (Ruby)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Nkerkar (talk | contribs) at 17:31, 7 February 2016 (Undid revision 703788729 by Nkerkar (talk)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Geocoder (Ruby)

Geocoder is a geocoding library for Ruby. When used with Rails, Geocoder adds geocoding functionality such as finding coordinates with street addresses or vice versa in addition to distance calculations for ActiveRecord objects.[1] Since the functionality does not rely on proprietary database functions, finding different geocoded objects in an area works out-of-the-box for databases like MySQL, PostgreSQL and SQLite.[1]

Compatability

Geocoder has been fully tested with Ruby 1.8.7, 1.9.2, and JRuby 1.5.3.[1]

Geocoder is also compatible with Rails 3. However, it there is limited functionality with Rails 2.[1]

Installation

Geocoder gem can be installed with the following command:

gem install geocoder

Or, if you're using Rails/Bundler, you may add this to your Gemfile:

gem 'geocoder'

and run at the command prompt:

bundle install[2]

It can be used as a plugin with rails too:

rails plugin install git://github.com/alexreisner/geocoder.git[3]

Configuration

In order to to use Geocoder with objects, the project must be set up as follows:

Required Attributes

ActiveRecord:

In order to use Geocoding with ActiveRecord objects, they must have two additional attributes, latitude and longitude coordinates.[1] When stored in the table they should be called latitude and longitude but they may be changed as explained below. When using reverse geocoding, the model must implement a method that returns an address. The address may be a single attribute; however, it can also be a method which returns a string assembled from different attributes such as city, state, and country.[1]

Mongoid:

When using Mongoid, the model only needs to add the address, latitude and longitudes as fields. The model must also include Geocoder::Model::Mongoid before making any calls to the geocoded_by: method. [1]

Model Behavior

In the model, Geocoder must be told which method returns the object's full address:

geocoded_by: :full_street_address     #can also be an IP address
after_validation :geocode       # auto-fetch coordinates

For reverse geocoding, Geocoder must know which method returns latitude and longitude coorindates

reverse_geocoded_by: :lat, :lon
after_validation: reverse_geocode     #auto-fetch address

In order to use different names for latitude and longitude in the model, the following change may be done when implementing geocoded_by:

geocoded_by :address, :latitude => :lat, :longitude => :lon

Additionally, the address method may return any string that would be used to search Google Maps. Any of the following examples will work:

Services

By default, Geocoder makes use of Google's geocoding API to retreive addresses and coordinates. However any of the following street address geocoding services are currently supported

  • Google: :google
  • Yahoo: :yahoo
  • Geocoder.ca: :geocoder_ca (US & Canada only)

Examples

Here are some examples to demonstrate Geocoder functionality:

Hotel.near("Raleigh, North Carolina")[3]

Finds hotels near Raleigh[3]

@restaurant.distance_to("Empire State Building")[3]

Finds the distance from @restaurant to the Empire State Building[3]

Other Uses

Developers may also use Geocoder to convert a user's IP Address to their city location. By making such a conversion you may be able to offer user's content relevant to their current location without requiring to ask for it beforehand.[4]

References

  1. ^ a b c d e f g "File: README — Documentation for rails-geocoder (0.9.11)". www.rubydoc.info. Retrieved 2016-01-31. {{cite web}}: line feed character in |title= at position 13 (help)
  2. ^ "alexreisner/geocoder:Complete ruby geocoding solution". www.github.com.
  3. ^ a b c d e "Ruby Geocoder". www.rubygeocoder.com. Retrieved 2016-01-31.
  4. ^ "Search by location with Geocoder". synbioz.com. Retrieved 2016-02-07.