`How to display city name received via ajax?
I
I
Ibishka2020-01-20 22:49:57
JavaScript
Ibishka, 2020-01-20 22:49:57

How to display city name received via ajax?

navigator.geolocation.getCurrentPosition(pos => {
        let xhr = new XMLHttpRequest();
        xhr.open(
          "GET",
          `https://geocode-maps.yandex.ru/1.x/?apikey=6792a574-8840-4ac0-8a9f-e6b7d21a5f69&geocode=${pos.coords.longitude},${pos.coords.latitude}&format=json&kind=locality&lang=en-US`
        );
        xhr.send();
        xhr.addEventListener("load", () => {
          if (xhr.status == 200) {
            console.log(JSON.parse(xhr.response));
          }
        });
      });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pashenka, 2020-01-20
@Ibishka

let data = JSON.parse(xhr.response);
console.log(data.response.GeoObjectCollection.featureMember[0].GeoObject.name); // (description)

I gave an example with a zero city ( [0]), there are not 1, but four of them in the above response, see for yourself what you need: the first one or all of them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question