public function Geocoding::geocode in Yandex.Maps 8
Get geo data for string.
Parameters
string $geolocation_string: Name of geographical object.
Return value
array|null Geocoding array.
File
- src/
Geocoding.php, line 39
Class
- Geocoding
- Class Geocoding.
Namespace
Drupal\yamapsCode
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;
}