Class: Google::Geo

Inherits:
Object
  • Object
show all
Defined in:
lib/google/geo.rb

Overview

Google::Geo

A simple, elegant library for getting geocoding information from Google Maps. Very much inspired by the google-geocode gem, but completely dependency free!

Examples

geo = Google::Geo.new API_KEY

address = geo.locate '1600 Amphitheatre Parkway, Mountain View, CA'

address.country      # 'US'
address.city         # 'Mountain View'
address.full_address # '1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA'

address.query        # '1600 Amphitheatre Parkway, Mountain View, CA'
address.accuracy     # 8

In the case of sufficiently vague queries, Google::Geo will return an Array:

addresses = geo.locate 'hell'

addresses.size                  # 2
addresses.map { |a| a.country } # ['US', 'NO']

Contributors

Seth Thomas Rasmussen - sethrasmussen.com - [email protected]

Defined Under Namespace

Modules: Parser Classes: Address, AddressError, Error, InvalidMapKeyError, MapKeyError, MissingAddressError, Response, ServerError, TooManyQueriesError, UnavailableAddressError, UnknownAddressError, UnknownError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Geo

Returns a new instance of Geo.



36
37
38
# File 'lib/google/geo.rb', line 36

def initialize(key)
  @key = key
end

Instance Attribute Details

#keyObject

API key provided by Google allowing access to the Geocode API



34
35
36
# File 'lib/google/geo.rb', line 34

def key
  @key
end

Instance Method Details

#locate(query) ⇒ Object

Returns an Address object with accessors for all the components of a location. The query argument should be a string.



42
43
44
45
46
47
# File 'lib/google/geo.rb', line 42

def locate(query)
  xml = open(uri(query)).read
  res = Response.new(xml, key)
  
  res.placemarks.map { |place| Address.new place, res.query }
end