M
M
mirexdoors2019-08-15 23:27:35
Yandex
mirexdoors, 2019-08-15 23:27:35

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();
        }

And this is how I call it:
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

1 answer(s)
A
Anton Shamanov, 2019-08-16
@mirexdoors

coordinates that at you whence undertake in pass?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question