Answer the question
In order to leave comments, you need to log in
How to access the Yandex.Organizations API several times in a row?
Hello!
I have a task: to get the coordinates of organizations for search queries and display them on the map. And if everything is fine with one request, then with several requests in a row, only the first one returns the result. Tried sending them in a loop with a timeout - same thing.
Function with request:
function getOrganizations(query, coords) {
var url = "https://search-maps.yandex.ru/v1/?text=" + query + "&ll=" + coords + "&spn=0.792069,0.680552&type=biz&lang=ru_RU&apikey=<?= YANDEX_SPRAVOCHNIK_KEY ?>";
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
console.log(xhr.response); // xhr.response.features - пустой при второй итерации
}
};
xhr.send();
}
function pass(rubric) {
return getOrganizations(rubric, coords);
}
var rubrics = ["кинотеатр", "кафе"];
for (var i = 0; i < rubrics.length; i++) {
setTimeout(pass(rubrics[i]), i*250);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question