D
D
Denis Afonin2021-07-27 17:14:28
Yandex maps
Denis Afonin, 2021-07-27 17:14:28

How to pass your parameter for Yandex multiple geocoding?

There is a list of addresses in an array with an additional status field . It is necessary to display a map on the site with marks and flags of a certain color depending on the status. I get coordinates through geocoding, everything is displayed on the map through a collection of labels. And how can you pass inside geocoder.then(); parameter unique for each iteration?
I tried to get the current element of the array from within the function by increasing the global variable , but it turns out that the coordinates and labels do not match the flags.

Code example:

// Создаем коллекцию.
        myAdressCollection = new ymaps.GeoObjectCollection();
        // Создаем массив с данными.

    myAdressPoints = [
      { adress: 'Сыктывкар, ул.Новый Север, 19/1', status : '1'},
      { adress: 'Сыктывкар, ул.Пальшина, 16', status : '1'},
      { adress: 'Сыктывкар, ул.Пионерская, 35', status : '1'},
      { adress: 'Сыктывкар, с.Выльгорт, ул.Домны Каликовой, 107/1а', status : '3'},
      { adress: 'Сыктывкар, ул.Шудалун, 38', status : '1'},
      { adress: 'Сыктывкар, ул.Ольховая, 35', status : '1'}
        ];



 // Заполняем коллекцию данными.
    for (var i = 0, l = myAdressPoints.length; i < l; i++) {
        var point = myAdressPoints[i];
    geocoder = ymaps.geocode(point.adress);

geocoder.then(
  function (res) {
         var coordinates = res.geoObjects.get(0).geometry.getCoordinates();
    addr = myAdressPoints[a].adress;
    m = myAdressPoints[a].status;
    if (m == '1'){metka='/single-window-service/tselevaya-model-podklyuchenie-k-setyam-gazoraspredeleniya/red_flag.png'};
    if (m == '2'){metka='/single-window-service/tselevaya-model-podklyuchenie-k-setyam-gazoraspredeleniya/green_flag.png'};
    if (m == '3'){metka='/single-window-service/tselevaya-model-podklyuchenie-k-setyam-gazoraspredeleniya/blue_flag.png'};


    console.log("Координаты:", a);
            // Добавление метки (Placemark) на карту
        myAdressCollection.add(new ymaps.Placemark(
            coordinates, {
          'hintContent': addr -Сюда нужно вставить адрес из массива
            }, {
          'iconLayout': 'default#image',
          'iconImageHref': '/single-window-service/tselevaya-model-podklyuchenie-k-setyam-gazoraspredeleniya/red_flag.png',
            		'iconImageSize': [20, 30],
            		'iconImageOffset': [-5, -38]
            }
        ));
    a++;
    }
  );
    }
    // Добавляем коллекцию меток на карту.
  map.geoObjects.add(myAdressCollection);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2021-07-27
@freeExec

You have the source address both in your array and in the Yandex response. You compare it.

D
Denis Afonin, 2021-07-28
@Afdenis

The problem was solved in the following way:
You need to replace for (var i = 0 with for (let i = 0 . The point is asynchrony.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question