class GoogleMaps::Services::ReverseGeocode
Performs requests to the Google Maps Geocoding API.
@example
reverse_geocode = GoogleMaps::Services::ReverseGeocode.new(client) result = reverse_geocode.query(latlng: {:lat => 52.520645, :lng => 13.409779}, language: "fr")
Attributes
client[RW]
Public Class Methods
new(client)
click to toggle source
# File lib/googlemaps/services/geocoding.rb, line 79 def initialize(client) self.client = client end
Public Instance Methods
query(latlng:, result_type: nil, location_type: nil, language: nil)
click to toggle source
Reverse geocoding is the process of converting geographic coordinates into a human-readable address.
@param [String, Hash] latlng The lat/lng value or place_id for which you wish to obtain the closest, human-readable address. @param [Array] result_type One or more address types to restrict results to. @param [Array] location_type One or more location types to restrict results to. @param [String] language The language in which to return results.
@return [String] Valid JSON or XML response.
# File lib/googlemaps/services/geocoding.rb, line 91 def query(latlng:, result_type: nil, location_type: nil, language: nil) # Check if latlng param is a place_id string. # 'place_id' strings do not contain commas; latlng strings do. if latlng.is_a?(String) && !latlng.include?(",") params = {'place_id' => latlng} else params = {'latlng' => Convert.to_latlng(latlng)} end if result_type params['result_type'] = Convert.join_array('|', result_type) end if location_type params['location_type'] = Convert.join_array('|', location_type) 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