Module: GoogleMapsService::Geocoding

Included in:
Client
Defined in:
lib/google_maps_service/geocoding.rb

Overview

Performs requests to the Google Maps Geocoding API.

Instance Method Summary collapse

Instance Method Details

#geocode(address: nil, components: nil, bounds: nil, region: nil, language: nil) ⇒ Object

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.

Parameters:

  • address (String) (defaults to: nil)

    The address to geocode.

  • components (Hash) (defaults to: nil)

    A component filter for which you wish to obtain a geocode, for example: ‘{ ’administrative_area’: ‘TX’,‘country’: ‘US’ }‘

  • bounds (String, Hash) (defaults to: nil)

    The bounding box of the viewport within which to bias geocode results more prominently. Accept string or hash with northeast and southwest keys.

  • region (String) (defaults to: nil)

    The region code, specified as a ccTLD (“top-level domain”) two-character value.

  • language (String) (defaults to: nil)

    The language in which to return results.

Returns:

  • Array of geocoding results.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/google_maps_service/geocoding.rb', line 23

def geocode(address: nil, components: nil, bounds: nil, region: nil, language: nil)
  params = {}

  params[:address] = address if address
  params[:components] = GoogleMapsService::Convert.components(components) if components
  params[:bounds] = GoogleMapsService::Convert.bounds(bounds) if bounds
  params[:region] = region if region
  params[:language] = language if language

  return get('/maps/api/geocode/json', params)[:results]
end

#reverse_geocode(latlng: nil, result_type: nil, location_type: nil, language: nil) ⇒ Object

Reverse geocoding is the process of converting geographic coordinates into a human-readable address.

Parameters:

  • latlng (Hash, Array) (defaults to: nil)

    The latitude/longitude value for which you wish to obtain the closest, human-readable address#

  • result_type (String, Array<String>) (defaults to: nil)

    One or more address types to restrict results to.

  • location_type (Array<String>) (defaults to: nil)

    One or more location types to restrict results to.

  • language (String) (defaults to: nil)

    The language in which to return results.

Returns:

  • Array of reverse geocoding results.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/google_maps_service/geocoding.rb', line 45

def reverse_geocode(latlng: nil, result_type: nil, location_type: nil, language: nil)
  params = {
    latlng: GoogleMapsService::Convert.latlng(latlng)
  }

  params[:result_type] = GoogleMapsService::Convert.join_list("|", result_type) if result_type
  params[:location_type] = GoogleMapsService::Convert.join_list("|", location_type) if location_type
  params[:language] = language if language

  return get('/maps/api/geocode/json', params)[:results]
end