Class: Drifter::Geocoders::Google

Inherits:
Base
  • Object
show all
Defined in:
lib/drifter/geocoders/google.rb

Overview

This class adds support for Google’s geocoding API: code.google.com/apis/maps/documentation/geocoding/

Constant Summary collapse

GOOGLE_BASE_URI =
'http://maps.googleapis.com/maps/api/geocode/json'

Class Method Summary collapse

Methods inherited from Base

last_error

Class Method Details

.base_uriObject

nodoc



14
15
16
# File 'lib/drifter/geocoders/google.rb', line 14

def self.base_uri
  GOOGLE_BASE_URI
end

.geocode(location, params = {}) ⇒ Object

This method works exactly like Drifter::Geocoders::Yahoo.geocode() See that method for more info. The returned Drifter::Location objects have the following attributes:

:address, :city, :state, :state_code, :country, :country_code,
:post_code, :lat, :lng

Additional google specific attributes can be accessed using the Location object’s data() method



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/drifter/geocoders/google.rb', line 28

def self.geocode(location, params={})

  params[:address] = location

  # check for reverse gecoding
  lat, lng = Drifter.extract_latlng(location)
  if lat && lng
    params.delete(:address)
    params[:latlng] = [lat, lng].join(',')
  end

  uri = query_uri(params)
  response = fetch(uri)

  # check for errors and return if necassary
  doc = JSON.parse(response)
  unless ["OK", "ZERO_RESULTS"].include?(doc["status"])
    @@last_error = { :code => doc["status"], :message => doc["status"] }
    return nil
  end

  # still here so safe to clear errors
  @@last_error = nil

  # is there anything to parse?
  return [] if doc["status"] == "ZERO_RESULTS"

  doc["results"].collect do |result|
    loc = Drifter::Location.new
    loc.raw_data_format = :hash
    loc.raw_data = result
    loc.geocoder = :google

    loc.address = result["formatted_address"]
    loc.lat = result["geometry"]["location"]["lat"]
    loc.lng = result["geometry"]["location"]["lng"]

    result["address_components"].each do |comp|
      loc.country_code = comp["short_name"] if comp["types"].include?("country")
      loc.country = comp["long_name"] if comp["types"].include?("country")
      loc.city = comp["long_name"] if comp["types"].include?("locality")
      loc.post_code = comp["long_name"] if comp["types"].include?("postal_code")
      loc.state = comp["long_name"] if comp["types"].include?("administrative_area_level_1")
      loc.state_code = comp["short_name"] if comp["types"].include?("administrative_area_level_1")
    end
    loc
  end

end

.query_uri(params = {}) ⇒ Object

Google requires a ‘sensor’ parameter. If none is set, it defaults to false See their docs for more info



81
82
83
84
85
86
# File 'lib/drifter/geocoders/google.rb', line 81

def self.query_uri(params={})
  params[:sensor] ||= 'false'
  uri = URI.parse(base_uri)
  uri.query = hash_to_query_string(params)
  return uri
end