Class: GoogleMaps::Services::Geocode
- Inherits:
-
Object
- Object
- GoogleMaps::Services::Geocode
- Defined in:
- lib/googlemaps/services/geocoding.rb
Overview
Performs requests to the Google Maps Geocoding API.
Instance Attribute Summary collapse
-
#client ⇒ Symbol
The HTTP client.
Instance Method Summary collapse
-
#initialize(client) ⇒ Geocode
constructor
A new instance of Geocode.
-
#query(address: nil, components: nil, bounds: nil, region: nil, language: nil) ⇒ Array, Nokogiri::XML::NodeSet
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.
Constructor Details
#initialize(client) ⇒ Geocode
Returns a new instance of Geocode.
16 17 18 |
# File 'lib/googlemaps/services/geocoding.rb', line 16 def initialize(client) self.client = client end |
Instance Attribute Details
#client ⇒ Symbol
Returns The HTTP client.
14 15 16 |
# File 'lib/googlemaps/services/geocoding.rb', line 14 def client @client end |
Instance Method Details
#query(address: nil, components: nil, bounds: nil, region: nil, language: nil) ⇒ Array, Nokogiri::XML::NodeSet
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.
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 |
# 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 |