class GoogleMaps::Services::Geocode
Performs requests to the Google Maps Geocoding API.
@example
geocode = GoogleMaps::Services::Geocode.new(client) result = geocode.query(address: "1600 Amphitheatre Parkway, Mountain View, CA")
Attributes
@return [Symbol] The HTTP client.
Public Class Methods
# File lib/googlemaps/services/geocoding.rb, line 16 def initialize(client) self.client = client end
Public Instance Methods
Geocoding is the process of converting addresses (like “1600 Amphitheatre Parkway, Mountain View, CA”) into geographic coordinates (like latitude 37.423021 and longitude -122.083739), which you can use to place markers or position the map.
@param [String] address The address to geocode. @param [Hash] components A component filter for which you wish to obtain a geocode.
E.g. {'administrative_area': 'TX','country': 'US'}
@param [Hash] bounds The bounding box of the viewport within which to bias geocode results more prominently.
The hash must have :northeast and :southwest keys.
@param [String] region The region code, specified as a ccTLD (“top-level domain”) two-character value. @param [String] language The language in which to return results.
@return [Array, Nokogiri::XML::NodeSet] Valid JSON or XML response.
# File lib/googlemaps/services/geocoding.rb, line 33 def query(address: nil, components: nil, bounds: nil, region: nil, language: nil) params = {} if address params['address'] = address end if components params['components'] = Convert.components(components) end if bounds params['bounds'] = Convert.bounds(bounds) end if region params['region'] = region end if language params['language'] = language end case self.client.response_format when :xml self.client .request(url: '/maps/api/geocode/xml', params: params) .xpath('//result') when :json self.client .request(url: '/maps/api/geocode/json', params: params) .fetch('results', []) else raise StandardError, 'Unsupported response format. Should be either :json or :xml.' end end