Class: Graticule::Geocoder::Google

Inherits:
Rest
  • Object
show all
Defined in:
lib/graticule/geocoder/google.rb

Overview

First you need a Google Maps API key. You can register for one here: www.google.com/apis/maps/signup.html

gg = Graticule.service(:google).new(MAPS_API_KEY)
location = gg.locate '1600 Amphitheater Pkwy, Mountain View, CA'
p location.coordinates
#=> [37.423111, -122.081783

Constant Summary collapse

PRECISION =
{
  0 => Precision.unknown,  # Unknown location. (Since 2.59)
  1 => Precision.country,  # Country level accuracy. (Since 2.59)
  2 => Precision.state,    # Region (state, province, prefecture, etc.) level accuracy. (Since 2.59)
  3 => Precision.state,    # Sub-region (county, municipality, etc.) level accuracy. (Since 2.59)
  4 => Precision.city,     # Town (city, village) level accuracy. (Since 2.59)
  5 => Precision.zip,      # Post code (zip code) level accuracy. (Since 2.59)
  6 => Precision.street,   # Street level accuracy. (Since 2.59)
  7 => Precision.street,   # Intersection level accuracy. (Since 2.59)
  8 => Precision.address,  # Address level accuracy. (Since 2.59)
  9 => Precision.premise   # Premise (building name, property name, shopping center, etc.) level accuracy. (Since 2.105)
}

Constants inherited from Base

Base::USER_AGENT

Instance Attribute Summary

Attributes inherited from Base

#preference

Instance Method Summary collapse

Methods inherited from Base

#<=>

Constructor Details

#initialize(key) ⇒ Google

Creates a new GoogleGeocode that will use Google Maps API key.



31
32
33
34
# File 'lib/graticule/geocoder/google.rb', line 31

def initialize(key)
  @key = key
  @url = URI.parse 'http://maps.google.com/maps/geo'
end

Instance Method Details

#locate(*args) ⇒ Object

Locates address returning a Location



37
38
39
40
41
42
43
# File 'lib/graticule/geocoder/google.rb', line 37

def locate(*args)
  case args.first
    when :first then get :first, :q => extract_location(args[1]) 
    when :all then get :all, :q => extract_location(args[1])
    else get :first, :q => extract_location(args.first)
  end
end