public function Geocoder::geocode in Geocoder 8.3
Same name and namespace in other branches
- 8.2 src/Geocoder.php \Drupal\geocoder\Geocoder::geocode()
- 7.2 src/Geocoder.php \Drupal\geocoder\Geocoder::geocode()
Geocodes a string.
Parameters
string $address_string: The string to geocode.
\Drupal\geocoder\GeocoderProviderInterface[] $providers: A list of Geocoder providers to use to perform the geocoding.
Return value
\Geocoder\Model\AddressCollection|\Geometry|null An address collection or NULL on geocoding failure.
Overrides GeocoderInterface::geocode
File
- src/
Geocoder.php, line 56
Class
- Geocoder
- Provides a geocoder factory class.
Namespace
Drupal\geocoderCode
public function geocode(string $address_string, array $providers) {
// Allow others modules to adjust the address string.
$this->moduleHandler
->alter('geocode_address_string', $address_string);
/** @var \Drupal\geocoder\GeocoderProviderInterface $provider */
foreach ($providers as $provider) {
try {
$result = $provider
->getPlugin()
->geocode($address_string);
if (!isset($result) || $result
->isEmpty()) {
throw new \Exception(sprintf('Unable to geocode "%s" with the %s provider.', $address_string, $provider
->id()));
}
return $result;
} catch (\Exception $e) {
static::log($e
->getMessage());
}
}
return NULL;
}