You are here

class Yandex in Geolocation Field 8.2

Same name in this branch
  1. 8.2 modules/geolocation_yandex/src/Plugin/geolocation/MapProvider/Yandex.php \Drupal\geolocation_yandex\Plugin\geolocation\MapProvider\Yandex
  2. 8.2 modules/geolocation_yandex/src/Plugin/geolocation/Geocoder/Yandex.php \Drupal\geolocation_yandex\Plugin\geolocation\Geocoder\Yandex
Same name and namespace in other branches
  1. 8.3 modules/geolocation_yandex/src/Plugin/geolocation/Geocoder/Yandex.php \Drupal\geolocation_yandex\Plugin\geolocation\Geocoder\Yandex

Provides the Yandex.

Plugin annotation


@Geocoder(
  id = "yandex",
  name = @Translation("Yandex"),
  description = @Translation("See https://tech.yandex.com/maps/doc/geocoder/desc/concepts/about-docpage/ for details."),
  locationCapable = true,
  boundaryCapable = true,
  frontendCapable = true,
)

Hierarchy

Expanded class hierarchy of Yandex

File

modules/geolocation_yandex/src/Plugin/geolocation/Geocoder/Yandex.php, line 24

Namespace

Drupal\geolocation_yandex\Plugin\geolocation\Geocoder
View source
class Yandex extends GeocoderBase implements GeocoderInterface {

  /**
   * {@inheritdoc}
   */
  public function formAttachGeocoder(array &$render_array, $element_name) {
    parent::formAttachGeocoder($render_array, $element_name);
    $render_array['#attached'] = BubbleableMetadata::mergeAttachments(empty($render_array['#attached']) ? [] : $render_array['#attached'], [
      'drupalSettings' => [
        'geolocation' => [
          'geocoder' => [
            $this
              ->getPluginId() => [],
          ],
        ],
      ],
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function geocode($address) {
    if (empty($address)) {
      return FALSE;
    }
    $config = \Drupal::config('yandex_maps.settings');
    $lang = \Drupal::languageManager()
      ->getCurrentLanguage()
      ->getId();
    $url = Url::fromUri('https://geocode-maps.yandex.ru/1.x/', [
      'query' => [
        'geocode' => $address,
        'format' => 'json',
        'apikey' => $config
          ->get('key'),
        'lang' => strtolower($lang) . '_' . strtoupper($lang),
      ],
    ]);
    try {
      $result = Json::decode(\Drupal::httpClient()
        ->get($url
        ->toString())
        ->getBody());
    } catch (RequestException $e) {
      watchdog_exception('geolocation', $e);
      return FALSE;
    }
    $location = [];
    if (empty($result['response']['GeoObjectCollection']['featureMember'][0])) {
      return FALSE;
    }
    else {
      $coordinates = explode(' ', $result['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['Point']['pos']);
      $location['location'] = [
        'lat' => $coordinates[0],
        'lng' => $coordinates[1],
      ];
    }
    if (!empty($result['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['boundedBy']['Envelope'])) {
      $lowerCoordinates = explode(' ', $result['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['boundedBy']['Envelope']['lowerCorner']);
      $upperCoordinates = explode(' ', $result['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['boundedBy']['Envelope']['upperCorner']);
      $location['boundary'] = [
        'lat_north_east' => $upperCoordinates[0],
        'lng_north_east' => $upperCoordinates[1],
        'lat_south_west' => $lowerCoordinates[0],
        'lng_south_west' => $lowerCoordinates[1],
      ];
    }
    if (!empty($result['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['description'])) {
      $location['address'] = $result['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['description'];
    }
    return $location;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
GeocoderBase::$countryFormatterManager protected property Country formatter manager.
GeocoderBase::addressElements protected function Get formatted address elements from atomics.
GeocoderBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
GeocoderBase::getDefaultSettings protected function Return plugin default settings. 2
GeocoderBase::getOptionsForm public function Return additional options form. Overrides GeocoderInterface::getOptionsForm 2
GeocoderBase::getSettings public function Return plugin settings.
GeocoderBase::processOptionsForm public function Process the form built above. Overrides GeocoderInterface::processOptionsForm
GeocoderBase::reverseGeocode public function Reverse geocode an address. Overrides GeocoderInterface::reverseGeocode 2
GeocoderBase::__construct public function GoogleGeocoderBase constructor. Overrides PluginBase::__construct 1
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
Yandex::formAttachGeocoder public function Attach geocoding logic to input element. Overrides GeocoderBase::formAttachGeocoder
Yandex::geocode public function Geocode an address. Overrides GeocoderBase::geocode