You are here

function yamaps_geocoding in Yandex.Maps 7

Get geo data for string.

Parameters

string $geolocation_string: Name of geographical object.

Return value

array|bool Geocoding array.

1 call to yamaps_geocoding()
yamaps_views_plugin_style_default_map::render in modules/yamaps_views/handlers/yamaps_views_plugin_style_default_map.inc
Renders the map.

File

./yamaps.functions.inc, line 36
Yandex Maps functions used across all components of the module.

Code

function yamaps_geocoding($geolocation_string) {
  if (!$geolocation_string) {
    return FALSE;
  }

  // Preparing geocoding string.
  $query = [
    'format' => 'json',
    'geocode' => $geolocation_string,
    'results' => 1,
    'lang' => 'ru',
  ];
  $geolocation_url = url(YAMAPS_GEOCODER_URL, [
    'query' => $query,
    'absolute' => TRUE,
  ]);
  $geolocation_request = drupal_http_request($geolocation_url);
  if (!empty($geolocation_request->data)) {
    $geolocation_data = json_decode($geolocation_request->data);
    if ($geolocation_data->response->GeoObjectCollection->metaDataProperty->GeocoderResponseMetaData->found > 0) {
      $map_center = $geolocation_data->response->GeoObjectCollection->featureMember[0]->GeoObject->Point->pos;
      $bounded_by = [
        array_map('yamaps_var_to_float', array_reverse(explode(' ', $geolocation_data->response->GeoObjectCollection->featureMember[0]->GeoObject->boundedBy->Envelope->lowerCorner))),
        array_map('yamaps_var_to_float', array_reverse(explode(' ', $geolocation_data->response->GeoObjectCollection->featureMember[0]->GeoObject->boundedBy->Envelope->upperCorner))),
      ];
      return [
        'map_center' => array_map('yamaps_var_to_float', array_reverse(explode(' ', $map_center))),
        'bounds' => $bounded_by,
      ];
    }
    else {
      return FALSE;
    }
  }
  else {
    return FALSE;
  }
}