You are here

protected function GoogleGeocodingAPI::getAddressAtomics in Geolocation Field 8.3

Gets array of geo data by Google Api result.

Parameters

array $result: Google API result array.

Return value

array Sorted array of geo data.

2 calls to GoogleGeocodingAPI::getAddressAtomics()
GoogleGeocodingAPI::geocode in modules/geolocation_google_maps/src/Plugin/geolocation/Geocoder/GoogleGeocodingAPI.php
Geocode an address.
GoogleGeocodingAPI::reverseGeocode in modules/geolocation_google_maps/src/Plugin/geolocation/Geocoder/GoogleGeocodingAPI.php
Reverse geocode an address.

File

modules/geolocation_google_maps/src/Plugin/geolocation/Geocoder/GoogleGeocodingAPI.php, line 244

Class

GoogleGeocodingAPI
Provides the Google Geocoding API.

Namespace

Drupal\geolocation_google_maps\Plugin\geolocation\Geocoder

Code

protected function getAddressAtomics(array $result) : array {
  $addressAtomicsMapping = [
    'streetNumber' => [
      'type' => 'street_number',
    ],
    'route' => [
      'type' => 'route',
    ],
    'locality' => [
      'type' => 'locality',
    ],
    'county' => [
      'type' => 'administrative_area_level_2',
    ],
    'countyCode' => [
      'type' => 'administrative_area_level_2',
      'short' => TRUE,
    ],
    'postalCode' => [
      'type' => 'postal_code',
    ],
    'administrativeArea' => [
      'type' => 'administrative_area_level_1',
      'short' => TRUE,
    ],
    'administrativeAreaLong' => [
      'type' => 'administrative_area_level_1',
    ],
    'country' => [
      'type' => 'country',
    ],
    'countryCode' => [
      'type' => 'country',
      'short' => TRUE,
    ],
    'postalTown' => [
      'type' => 'postal_town',
    ],
    'neighborhood' => [
      'type' => 'neighborhood',
    ],
    'premise' => [
      'type' => 'premise',
    ],
    'political' => [
      'type' => 'political',
    ],
  ];
  $address_atomics = [];
  foreach ($result['results'][0]['address_components'] as $component) {
    foreach ($addressAtomicsMapping as $atomic => $google_format) {
      if ($google_format['type'] == $component['types'][0]) {
        if (!empty($google_format['short'])) {
          $address_atomics[$atomic] = $component['short_name'];
        }
        else {
          $address_atomics[$atomic] = $component['long_name'];
        }
      }
    }
  }
  return $address_atomics;
}