You are here

public function GoogleGeocodingAPI::geocode in Geolocation Field 8.2

Same name and namespace in other branches
  1. 8.3 modules/geolocation_google_maps/src/Plugin/geolocation/Geocoder/GoogleGeocodingAPI.php \Drupal\geolocation_google_maps\Plugin\geolocation\Geocoder\GoogleGeocodingAPI::geocode()

Geocode an address.

Parameters

string $address: Address to geocode.

Return value

array||null Location or NULL.

Overrides GeocoderBase::geocode

File

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

Class

GoogleGeocodingAPI
Provides the Google Geocoding API.

Namespace

Drupal\geolocation_google_maps\Plugin\geolocation\Geocoder

Code

public function geocode($address) {
  $config = \Drupal::config('geolocation_google_maps.settings');
  if (empty($address)) {
    return FALSE;
  }
  if (!empty($config
    ->get('google_maps_base_url'))) {
    $request_url = $config
      ->get('google_maps_base_url');
  }
  elseif ($config
    ->get('china_mode')) {
    $request_url = GoogleMaps::$GOOGLEMAPSAPIURLBASECHINA;
  }
  else {
    $request_url = GoogleMaps::$GOOGLEMAPSAPIURLBASE;
  }
  $request_url .= '/maps/api/geocode/json?address=' . urlencode($address);
  if (!empty($config
    ->get('google_map_api_server_key'))) {
    $request_url .= '&key=' . $config
      ->get('google_map_api_server_key');
  }
  elseif (!empty($config
    ->get('google_map_api_key'))) {
    $request_url .= '&key=' . $config
      ->get('google_map_api_key');
  }
  if (!empty($this->configuration['component_restrictions'])) {
    $request_url .= '&components=';
    foreach ($this->configuration['component_restrictions'] as $component_id => $component_value) {
      $request_url .= $component_id . ':' . $component_value . '|';
    }
  }
  if (!empty($config
    ->get('google_map_custom_url_parameters')['language'])) {
    $request_url .= '&language=' . $config
      ->get('google_map_custom_url_parameters')['language'];
  }
  if (!empty($this->configuration['boundary_restriction']) && !empty($this->configuration['boundary_restriction']['south']) && !empty($this->configuration['boundary_restriction']['west']) && !empty($this->configuration['boundary_restriction']['north']) && !empty($this->configuration['boundary_restriction']['east'])) {
    $bounds = $this->configuration['boundary_restriction']['south'] . ',';
    $bounds .= $this->configuration['boundary_restriction']['west'] . '|';
    $bounds .= $this->configuration['boundary_restriction']['north'] . ',';
    $bounds .= $this->configuration['boundary_restriction']['east'];
    $request_url .= '&bounds=' . $bounds;
  }
  try {
    $result = Json::decode(\Drupal::httpClient()
      ->request('GET', $request_url)
      ->getBody());
  } catch (RequestException $e) {
    watchdog_exception('geolocation', $e);
    return FALSE;
  }
  if ($result['status'] != 'OK' || empty($result['results'][0]['geometry'])) {
    if (isset($result['error_message'])) {
      \Drupal::logger('geolocation')
        ->error(t('Unable to geocode "@address" with error: "@error". Request URL: @url', [
        '@address' => $address,
        '@error' => $result['error_message'],
        '@url' => $request_url,
      ]));
    }
    return FALSE;
  }
  return [
    'location' => [
      'lat' => $result['results'][0]['geometry']['location']['lat'],
      'lng' => $result['results'][0]['geometry']['location']['lng'],
    ],
    // TODO: Add viewport or build it if missing.
    'boundary' => [
      'lat_north_east' => empty($result['results'][0]['geometry']['viewport']) ? $result['results'][0]['geometry']['location']['lat'] + 0.005 : $result['results'][0]['geometry']['viewport']['northeast']['lat'],
      'lng_north_east' => empty($result['results'][0]['geometry']['viewport']) ? $result['results'][0]['geometry']['location']['lng'] + 0.005 : $result['results'][0]['geometry']['viewport']['northeast']['lng'],
      'lat_south_west' => empty($result['results'][0]['geometry']['viewport']) ? $result['results'][0]['geometry']['location']['lat'] - 0.005 : $result['results'][0]['geometry']['viewport']['southwest']['lat'],
      'lng_south_west' => empty($result['results'][0]['geometry']['viewport']) ? $result['results'][0]['geometry']['location']['lng'] - 0.005 : $result['results'][0]['geometry']['viewport']['southwest']['lng'],
    ],
    'address' => empty($result['results'][0]['formatted_address']) ? '' : $result['results'][0]['formatted_address'],
  ];
}