Answer the question
In order to leave comments, you need to log in
`
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
let data = JSON.parse(xhr.response);
console.log(data.response.GeoObjectCollection.featureMember[0].GeoObject.name); // (description)
[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 questionAsk a Question
731 491 924 answers to any question