I
I
Ivan2020-06-12 16:24:36
PHP
Ivan, 2020-06-12 16:24:36

Geocoder Yandex. How to get the nearest metro from an address in php?

I took all the info from here https://tech.yandex.ru/maps/geocoder/doc/desc/conc... I

got the token here - https://developer.tech.yandex.ru/services/
5ee381768efcc566911643.png

Here is the code.

$parameters = array(
      'apikey' => 'f731a89f-4492-43ff-a822-9de189c2f71e',
      'geocode' => 'Москва,+улица+Габричевского,+8',
      'kind' => 'metro',
      'results' => '1'
    );

    $response = file_get_contents('https://geocode-maps.yandex.ru/1.x/?'. http_build_query($parameters));


Here is the answer:
5ee381d332a52646531445.png

If we take an example from the documentation.
https://geocode-maps.yandex.ru/1.x/?apikey=ваш API-ключ&geocode=37.611347,55.760241&kind=metro&results=1


It works and the metro shows. I looked where these coordinates are, they are generally in Iran.
And when I insert either coordinates or an address in my code, there is no subway.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2020-06-12
@youmixx

Found a solution. With regards to Iran, the coordinates on the map were simply not perceived correctly (there was 37.611347.55.760241, but it was necessary to change places 55.760241.37.611347, otherwise everything is fine).
It didn’t work for me, because for some reason at the address, he didn’t want to show the metro. But if you insert the coordinates, it showed. Therefore, we make the first request to get the coordinates, and the second - to get the metro.

$address = 'Самара,улица+22+Партсъезда,+15';
    $parameters = array(
      'apikey' => 'token',
      'geocode' => $address, # Самара,улица+22+Партсъезда,+15
      'format' => 'json'
    );

    $response = file_get_contents('https://geocode-maps.yandex.ru/1.x/?'. http_build_query($parameters));
    $obj = json_decode($response, true);

    $cord = str_replace(" ", ",", $obj['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['Point']['pos']);
    $parameters = array(
      'apikey' => 'token',
      'geocode' => $cord,
      'kind' => 'metro',
      'format' => 'json'
    );

    $response = file_get_contents('https://geocode-maps.yandex.ru/1.x/?'. http_build_query($parameters));
    $obj = json_decode($response, true);

    $metro = $obj['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['name'];
    echo $metro;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question