class Geocoding in Yandex.Maps 8
Class Geocoding.
Hierarchy
- class \Drupal\yamaps\Geocoding
Expanded class hierarchy of Geocoding
1 file declares its use of Geocoding
- YaMaps.php in src/
Plugin/ views/ style/ YaMaps.php
1 string reference to 'Geocoding'
1 service uses Geocoding
File
- src/
Geocoding.php, line 12
Namespace
Drupal\yamapsView source
class Geocoding {
public const YAMAPS_GEOCODER_URL = '//geocode-maps.yandex.ru/1.x/';
/**
* The psr7 Http.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* {@inheritdoc}
*/
public function __construct(Client $httpClient) {
$this->httpClient = $httpClient;
}
/**
* Get geo data for string.
*
* @param string $geolocation_string
* Name of geographical object.
*
* @return array|null
* Geocoding array.
*/
public function geocode($geolocation_string) : ?array {
if (!$geolocation_string) {
return NULL;
}
// Preparing geocoding string.
$query = [
'format' => 'json',
'geocode' => $geolocation_string,
'results' => 1,
'lang' => 'ru',
];
$geolocation_url = Url::fromUri(static::YAMAPS_GEOCODER_URL, [
'query' => $query,
'absolute' => TRUE,
]);
$geolocation_request = $this->httpClient
->get($geolocation_url
->toString());
$geolocation_data = Json::decode($geolocation_request
->getBody()
->getContents());
if ($geolocation_data && $geolocation_data['response']['GeoObjectCollection']['metaDataProperty']['GeocoderResponseMetaData']['found'] > 0) {
$map_center = $geolocation_data['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['Point']['pos'];
$bounded_by = [
\array_map('floatval', \array_reverse(\explode(' ', $geolocation_data['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['boundedBy']['Envelope']['lowerCorner']))),
\array_map('floatval', \array_reverse(\explode(' ', $geolocation_data['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['boundedBy']['Envelope']['upperCorner']))),
];
return [
'map_center' => \array_map('floatval', \array_reverse(\explode(' ', $map_center))),
'bounds' => $bounded_by,
];
}
return NULL;
}
/**
* Prepares values for js.
*
* @param array $params
* Params of map.
*
* @return array
* Prepared values.
*/
public function decodeParams(array $params) : array {
return [
'coords' => isset($params['coords']) ? Json::decode($params['coords']) : [],
'type' => !empty($params['coordstype']) ? $params['coordstype'] : 'yandex#map',
'placemarks' => Json::decode($params['placemarks']),
'lines' => Json::decode($params['lines']),
'polygons' => Json::decode($params['polygons']),
'routes' => Json::decode($params['routes']),
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Geocoding:: |
protected | property | The psr7 Http. | |
Geocoding:: |
public | function | Prepares values for js. | |
Geocoding:: |
public | function | Get geo data for string. | |
Geocoding:: |
public | constant | ||
Geocoding:: |
public | function |