You are here

class GeocoderJsonConsumer in Geocoder autocomplete 8

Defines the GeocoderJsonConsumer service, for return parse GeoJson.

Hierarchy

Expanded class hierarchy of GeocoderJsonConsumer

1 file declares its use of GeocoderJsonConsumer
GeocoderController.php in src/Controller/GeocoderController.php
1 string reference to 'GeocoderJsonConsumer'
geocoder_autocomplete.services.yml in ./geocoder_autocomplete.services.yml
geocoder_autocomplete.services.yml
1 service uses GeocoderJsonConsumer
geocoderautocomplete.consumer in ./geocoder_autocomplete.services.yml
Drupal\geocoder_autocomplete\GeocoderJsonConsumer

File

src/GeocoderJsonConsumer.php, line 12

Namespace

Drupal\geocoder_autocomplete
View source
class GeocoderJsonConsumer {

  /**
   * Drupal http client.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $httpClient;

  /**
   * Language Manager service.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * Service constructor.
   *
   * @param \GuzzleHttp\ClientInterface $http_client
   *   The http client.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   */
  public function __construct(ClientInterface $http_client, LanguageManagerInterface $language_manager) {
    $this->httpClient = $http_client;
    $this->languageManager = $language_manager;
  }

  /**
   * Return json list of geolocation matching $text.
   *
   * @param string $text
   *   The text query for search a place.
   *
   * @return array
   *   An array of matching location.
   */
  public function getAddress($text) {
    $language_interface = $this->languageManager
      ->getCurrentLanguage();
    $language = isset($language_interface) ? $language_interface
      ->getId() : 'en';
    $config = \Drupal::config('geocoder_autocomplete.settings');
    $query = [
      'address' => $text,
      'language' => $language,
      'sensor' => 'false',
      'region' => $config
        ->get('region_code_bias'),
    ];
    $uri = 'http://maps.googleapis.com/maps/api/geocode/json';
    $response = $this->httpClient
      ->request('GET', $uri, [
      'query' => $query,
    ]);
    $matches = [];
    if (empty($response->error)) {
      $data = json_decode($response
        ->getBody());
      if ($data->status == 'OK') {
        foreach ($data->results as $result) {
          if (!empty($result->formatted_address)) {
            $formatted_address = $result->formatted_address;
            $matches[] = [
              'value' => Html::escape($formatted_address),
              'label' => Html::escape($formatted_address),
            ];
          }
        }
      }
    }
    return $matches;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GeocoderJsonConsumer::$httpClient protected property Drupal http client.
GeocoderJsonConsumer::$languageManager protected property Language Manager service.
GeocoderJsonConsumer::getAddress public function Return json list of geolocation matching $text.
GeocoderJsonConsumer::__construct public function Service constructor.