class GooglePlacesAPI in Geolocation Field 8.3
Same name and namespace in other branches
- 8 modules/geolocation_google_places_api/src/Plugin/geolocation/Geocoder/GooglePlacesAPI.php \Drupal\geolocation_google_places_api\Plugin\geolocation\Geocoder\GooglePlacesAPI
- 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
Provides the Google Places API.
Plugin annotation
@Geocoder(
id = "google_places_api",
name = @Translation("Google Places API"),
description = @Translation("Attention: This Plugin needs you to follow Google Places API TOS and either use the Attribution Block or provide it yourself."),
locationCapable = true,
boundaryCapable = true,
frontendCapable = true,
reverseCapable = false,
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\geolocation\GeocoderBase implements ContainerFactoryPluginInterface, GeocoderInterface
- class \Drupal\geolocation_google_maps\GoogleGeocoderBase implements GeocoderInterface
- class \Drupal\geolocation_google_places_api\Plugin\geolocation\Geocoder\GooglePlacesAPI
- class \Drupal\geolocation_google_maps\GoogleGeocoderBase implements GeocoderInterface
- class \Drupal\geolocation\GeocoderBase implements ContainerFactoryPluginInterface, GeocoderInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of GooglePlacesAPI
File
- modules/
geolocation_google_maps/ modules/ geolocation_google_places_api/ src/ Plugin/ geolocation/ Geocoder/ GooglePlacesAPI.php, line 25
Namespace
Drupal\geolocation_google_places_api\Plugin\geolocation\GeocoderView source
class GooglePlacesAPI extends GoogleGeocoderBase {
/**
* {@inheritdoc}
*/
public function formAttachGeocoder(array &$render_array, $element_name) {
parent::formAttachGeocoder($render_array, $element_name);
$render_array['#attached'] = BubbleableMetadata::mergeAttachments($render_array['#attached'], [
'library' => [
'geolocation_google_places_api/geolocation_google_places_api.geocoder.googleplacesapi',
],
]);
}
/**
* {@inheritdoc}
*/
public function geocode($address) {
if (empty($address)) {
return FALSE;
}
$config = \Drupal::config('geolocation_google_maps.settings');
$request_url = GoogleMaps::$googleMapsApiUrlBase;
if ($config
->get('china_mode')) {
$request_url = GoogleMaps::$googleMapsApiUrlBaseChina;
}
$request_url .= '/maps/api/place/autocomplete/json?input=' . $address;
$google_key = '';
if (!empty($config
->get('google_map_api_server_key'))) {
$google_key = KeyProvider::getKeyValue($config
->get('google_map_api_server_key'));
}
elseif (!empty($config
->get('google_map_api_key'))) {
$google_key = KeyProvider::getKeyValue($config
->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($config
->get('google_map_custom_url_parameters')['language'])) {
$request_url .= '&language=' . $config
->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 {
if (!empty($config
->get('google_maps_base_url'))) {
$details_url = $config
->get('google_maps_base_url');
}
elseif ($config
->get('china_mode')) {
$details_url = GoogleMaps::$googleMapsApiUrlBaseChina;
}
else {
$details_url = GoogleMaps::$googleMapsApiUrlBase;
}
$details_url .= '/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'],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
GeocoderBase:: |
protected | property | Country formatter manager. | |
GeocoderBase:: |
protected | function | Get formatted address elements from atomics. | |
GeocoderBase:: |
public | function | Return plugin settings. | |
GeocoderBase:: |
public | function |
Process the form built above. Overrides GeocoderInterface:: |
|
GeocoderBase:: |
public | function |
Reverse geocode an address. Overrides GeocoderInterface:: |
3 |
GoogleGeocoderBase:: |
protected | property | Google maps provider. | |
GoogleGeocoderBase:: |
public static | function |
Creates an instance of the plugin. Overrides GeocoderBase:: |
|
GoogleGeocoderBase:: |
protected | function |
Return plugin default settings. Overrides GeocoderBase:: |
1 |
GoogleGeocoderBase:: |
public | function |
Return additional options form. Overrides GeocoderBase:: |
1 |
GoogleGeocoderBase:: |
public | function |
GoogleGeocoderBase constructor. Overrides GeocoderBase:: |
|
GooglePlacesAPI:: |
public | function |
Attach geocoding logic to input element. Overrides GoogleGeocoderBase:: |
|
GooglePlacesAPI:: |
public | function |
Geocode an address. Overrides GeocoderBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |