public function GooglePlacesAPI::geocode in Geolocation Field 8
Same name and namespace in other branches
- 8.3 modules/geolocation_google_maps/modules/geolocation_google_places_api/src/Plugin/geolocation/Geocoder/GooglePlacesAPI.php \Drupal\geolocation_google_places_api\Plugin\geolocation\Geocoder\GooglePlacesAPI::geocode()
- 8.2 modules/geolocation_google_maps/modules/geolocation_google_places_api/src/Plugin/geolocation/Geocoder/GooglePlacesAPI.php \Drupal\geolocation_google_places_api\Plugin\geolocation\Geocoder\GooglePlacesAPI::geocode()
Geocode an address.
Parameters
string $address: Address to geocode.
Return value
array||null Location or NULL.
Overrides GeocoderBase::geocode
2 calls to GooglePlacesAPI::geocode()
- GooglePlacesAPI::formProcessInput in modules/
geolocation_google_places_api/ src/ Plugin/ geolocation/ Geocoder/ GooglePlacesAPI.php - Process from as altered above.
- GooglePlacesAPI::formValidateInput in modules/
geolocation_google_places_api/ src/ Plugin/ geolocation/ Geocoder/ GooglePlacesAPI.php - Process from as altered above.
File
- modules/
geolocation_google_places_api/ src/ Plugin/ geolocation/ Geocoder/ GooglePlacesAPI.php, line 197
Class
- GooglePlacesAPI
- Provides the Google Places API.
Namespace
Drupal\geolocation_google_places_api\Plugin\geolocation\GeocoderCode
public function geocode($address) {
if (empty($address)) {
return FALSE;
}
$request_url = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=' . $address;
$google_key = '';
if (!empty($this->geolocationSettings
->get('google_map_api_server_key'))) {
$google_key = $this->geolocationSettings
->get('google_map_api_server_key');
}
elseif (!empty($this->geolocationSettings
->get('google_map_api_key'))) {
$google_key = $this->geolocationSettings
->get('google_map_api_key');
}
if (!empty($google_key)) {
$request_url .= '&key=' . $google_key;
}
if (!empty($this->configuration['component_restrictions']['country'])) {
$request_url .= '&components=country:' . $this->configuration['component_restrictions']['country'];
}
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['predictions'][0]['place_id'])) {
return FALSE;
}
try {
$details_url = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=' . $result['predictions'][0]['place_id'];
if (!empty($google_key)) {
$details_url .= '&key=' . $google_key;
}
$details = Json::decode(\Drupal::httpClient()
->request('GET', $details_url)
->getBody());
} catch (RequestException $e) {
watchdog_exception('geolocation', $e);
return FALSE;
}
if ($details['status'] != 'OK' || empty($details['result']['geometry']['location'])) {
return FALSE;
}
return [
'location' => [
'lat' => $details['result']['geometry']['location']['lat'],
'lng' => $details['result']['geometry']['location']['lng'],
],
// TODO: Add viewport or build it if missing.
'boundary' => [
'lat_north_east' => empty($details['result']['geometry']['viewport']) ? $details['result']['geometry']['location']['lat'] + 0.005 : $details['result']['geometry']['viewport']['northeast']['lat'],
'lng_north_east' => empty($details['result']['geometry']['viewport']) ? $details['result']['geometry']['location']['lng'] + 0.005 : $details['result']['geometry']['viewport']['northeast']['lng'],
'lat_south_west' => empty($details['result']['geometry']['viewport']) ? $details['result']['geometry']['location']['lat'] - 0.005 : $details['result']['geometry']['viewport']['southwest']['lat'],
'lng_south_west' => empty($details['result']['geometry']['viewport']) ? $details['result']['geometry']['location']['lng'] - 0.005 : $details['result']['geometry']['viewport']['southwest']['lng'],
],
'address' => empty($details['result']['formatted_address']) ? '' : $details['result']['formatted_address'],
];
}