public function GoogleGeocodingAPI::geocode in Geolocation Field 8.3
Same name and namespace in other branches
- 8.2 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 84
Class
- GoogleGeocodingAPI
- Provides the Google Geocoding API.
Namespace
Drupal\geolocation_google_maps\Plugin\geolocation\GeocoderCode
public function geocode($address) {
$config = \Drupal::config('geolocation_google_maps.settings');
$query_params = [
'address' => $address,
];
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 .= self::API_PATH;
if (!empty($config
->get('google_map_api_server_key'))) {
$query_params['key'] = KeyProvider::getKeyValue($config
->get('google_map_api_server_key'));
}
elseif (!empty($config
->get('google_map_api_key'))) {
$query_params['key'] = KeyProvider::getKeyValue($config
->get('google_map_api_key'));
}
if (!empty($this->configuration['component_restrictions'])) {
$components = [];
foreach ($this->configuration['component_restrictions'] as $component_id => $component_value) {
$components[] = $component_id . ':' . $component_value;
}
$query_params['components'] = implode('|', $components);
}
if (!empty($config
->get('google_map_custom_url_parameters')['language'])) {
$query_params['language'] = $config
->get('google_map_custom_url_parameters')['language'];
}
if (!empty($this->configuration['region'])) {
$query_params['region'] = $this->configuration['region'];
}
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'];
$query_params['bounds'] = $bounds;
}
try {
$result = Json::decode(\Drupal::httpClient()
->get($request_url, [
'query' => $query_params,
])
->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'],
'atomics' => $this
->getAddressAtomics($result),
];
}