You are here

protected function GeocodeOrigin::getAddressOrigin in Geocoder 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/GeofieldProximitySource/GeocodeOrigin.php \Drupal\geocoder\Plugin\GeofieldProximitySource\GeocodeOrigin::getAddressOrigin()

Geocode the Origin Address.

Parameters

string $address: The String address to Geocode.

Return value

array The Origin array.

2 calls to GeocodeOrigin::getAddressOrigin()
GeocodeOrigin::validateOptionsForm in src/Plugin/GeofieldProximitySource/GeocodeOrigin.php
Validates the options form for the geofield proximity plugin.
GeocodeOrigin::__construct in src/Plugin/GeofieldProximitySource/GeocodeOrigin.php
Constructs a GeocodeOrigin object.

File

src/Plugin/GeofieldProximitySource/GeocodeOrigin.php, line 156

Class

GeocodeOrigin
Defines 'Geocode Origin (with Autocomplete option)' proximity source plugin.

Namespace

Drupal\geocoder\Plugin\GeofieldProximitySource

Code

protected function getAddressOrigin($address) {
  $origin = [
    'lat' => '',
    'lon' => '',
  ];
  if (!empty($address)) {

    // Try static geocoding cache.
    $cache =& drupal_static("geocoder_proximity_cache:{$address}", NULL);
    if (is_array($cache) && array_key_exists('lat', $cache) && array_key_exists('lon', $cache)) {
      return $cache;
    }
    $provider_plugins = $this
      ->getEnabledProviderPlugins();

    // Try geocoding and extract coordinates of the first match.
    $address_collection = $this->geocoder
      ->geocode($address, GeocoderProvider::loadMultiple(array_keys($provider_plugins)));
    if ($address_collection instanceof AddressCollection && count($address_collection) > 0) {
      $address = $address_collection
        ->get(0);
      $coordinates = $address
        ->getCoordinates();
      $origin = [
        'lat' => $coordinates
          ->getLatitude(),
        'lon' => $coordinates
          ->getLongitude(),
      ];
    }
    $cache = $origin;
  }
  return $origin;
}