public function GoogleGeocodingAPI::geocode in Geolocation Field 8
Geocode an address.
Parameters
string $address: Address to geocode.
Return value
array||null Location or NULL.
Overrides GeocoderBase::geocode
2 calls to GoogleGeocodingAPI::geocode()
- GoogleGeocodingAPI::formProcessInput in src/
Plugin/ geolocation/ Geocoder/ GoogleGeocodingAPI.php - Process from as altered above.
- GoogleGeocodingAPI::formValidateInput in src/
Plugin/ geolocation/ Geocoder/ GoogleGeocodingAPI.php - Process from as altered above.
File
- src/
Plugin/ geolocation/ Geocoder/ GoogleGeocodingAPI.php, line 189
Class
- GoogleGeocodingAPI
- Provides the Google Geocoding API.
Namespace
Drupal\geolocation\Plugin\geolocation\GeocoderCode
public function geocode($address) {
if (empty($address)) {
return FALSE;
}
$request_url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . $address;
if (!empty($this->geolocationSettings
->get('google_map_api_server_key'))) {
$request_url .= '&key=' . $this->geolocationSettings
->get('google_map_api_server_key');
}
elseif (!empty($this->geolocationSettings
->get('google_map_api_key'))) {
$request_url .= '&key=' . $this->geolocationSettings
->get('google_map_api_key');
}
if (!empty($this->configuration['components'])) {
$request_url .= '&components=';
foreach ($this->configuration['components'] as $component_id => $component_value) {
$request_url .= $component_id . ':' . $component_value . '|';
}
}
if (!empty($this->geolocationSettings
->get('google_map_custom_url_parameters')['language'])) {
$request_url .= '&language=' . $this->geolocationSettings
->get('google_map_custom_url_parameters')['language'];
}
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".', [
'@address' => $address,
'@error' => $result['error_message'],
]));
}
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'],
];
}