Module: GoogleMapsService::Apis::Geocoding

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

Overview

Performs requests to the Google Maps Geocoding API.

Instance Method Summary collapse

Instance Method Details

#geocode(address, components: nil, bounds: nil, region: nil, language: nil, response_slice: :results) ⇒ Array

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.

Examples:

Geocode an address

results = client.geocode('Sydney')

Geocode a component only

results = client.geocode(nil, components: {administrative_area: 'TX', country: 'US'})

Geocode an address and component

results = client.geocode('Sydney', components: {administrative_area: 'TX', country: 'US'})

Multiple parameters

results = client.geocode('Sydney',
    components: {administrative_area: 'TX', country: 'US'},
    bounds: {
      northeast: {lat: 32.7183997, lng: -97.26864001970849},
      southwest: {lat: 32.7052583, lng: -97.27133798029149}
    },
    region: 'us')

Parameters:

  • address (String)

    The address to geocode. You must specify either this value and/or ‘components`.

  • components (Hash) (defaults to: nil)

    A component filter for which you wish to obtain a geocode, for example: ‘‘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.

  • response_slice (Symbol) (defaults to: :results)

    Specify subset of response to return. Defaults to :results for backwards compatibility. Use :all to get complete response.

Returns:

  • (Array)

    Array of geocoding results.



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

def geocode(address, components: nil, bounds: nil, region: nil, language: nil, response_slice: :results)
  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

  if response_slice == :all
    get("/maps/api/geocode/json", params)
  else
    get("/maps/api/geocode/json", params)[response_slice]
  end
end

#reverse_geocode(latlng, location_type: nil, result_type: nil, language: nil, response_slice: :results) ⇒ Array

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

Examples:

Simple lat/lon pair

client.reverse_geocode({lat: 40.714224, lng: -73.961452})

Multiple parameters

client.reverse_geocode([40.714224, -73.961452],
    location_type: ['ROOFTOP', 'RANGE_INTERPOLATED'],
    result_type: ['street_address', 'route'],
    language: 'es')

Parameters:

  • latlng (Hash, Array)

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

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

    One or more location types to restrict results to.

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

    One or more address types to restrict results to.

  • language (String) (defaults to: nil)

    The language in which to return results.

  • response_slice (Symbol) (defaults to: :results)

    Specify subset of response to return. Defaults to :results for backwards compatibility. Use :all to get complete response.

Returns:

  • (Array)

    Array of reverse geocoding results.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/google_maps_service/apis/geocoding.rb', line 78

def reverse_geocode(latlng, location_type: nil, result_type: nil, language: nil, response_slice: :results)
  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

  if response_slice == :all
    get("/maps/api/geocode/json", params)
  else
    get("/maps/api/geocode/json", params)[response_slice]
  end
end