S
S
smoln2022-01-26 10:12:35
API
smoln, 2022-01-26 10:12:35

How to show information when clicking on a Yandex map label?

Good afternoon, help me solve the issue with displaying information when clicking on the label
Here is the code

function init() {
        // Создаем карту
        myMap = new ymaps.Map("map", {
      center: [53.9,27.5667],
                zoom: 9,
            controls: [
                'zoomControl'
            ],
            zoomMargin: [20]
        });

        for (var i = 0; i < shopList.length; i++) {

            // Добавляем название города в выпадающий список
            $('select#cities').append('<option value="' + i + '">' + shopList[i].cityName + '</option>');

            // Создаём коллекцию меток для города
            var cityCollection = new ymaps.GeoObjectCollection();

            for (var c = 0; c < shopList[i].shops.length; c++) {
                var shopInfo = shopList[i].shops[c];

                var shopPlacemark = new ymaps.Placemark(
                    shopInfo.coordinates,
                    {
                        hintContent: shopInfo.name,
                        balloonContent: shopInfo.name,   
            WarehouseId: shopInfo.WarehouseId
                    }, 
          {

                         preset: 'islands#redDotIcon'

                   }
                );

                if (!placemarkList[i]) placemarkList[i] = {};
                placemarkList[i][c] = shopPlacemark;

                // Добавляем метку в коллекцию
                cityCollection.add(shopPlacemark);

            }
  

            placemarkCollections[i] = cityCollection;

            // Добавляем коллекцию на карту
            myMap.geoObjects.add(cityCollection);

        }




        $('select#cities').trigger('change');

    myMap.geoObjects.events.add('click', function (e) {

      console.log(shopPlacemark.properties.get('balloonContent'));  

        });

  
      

    }


I'm trying to display information on click on the following code

myMap.geoObjects.events.add('click', function (e) {

      console.log(shopPlacemark.properties.get('balloonContent'));  

        });

now when you click on any label, the information for the most recent label is displayed
If I do:
console.log(myMap.geoObjects.properties.get('balloonContent')); - error
console.log(e.get('balloonContent')); - undefined

how to make it work?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
smoln, 2022-01-26
@smoln

Having looked better at the documentation, came to a solution, first, through the target, we get a link to the object with parameters, then we pull out the parameter that we need

var target = e.get('target');
  console.log(target.properties.get('WarehouseId'));

S
sinneren, 2022-01-26
@sinneren

well, it seems logical that the latter, if we show shopPlacemark
https://yandex.ru/dev/maps/jsbox/2.1/geoobject_events
https://yandex.ru/dev/maps/jsbox/2.1/event_properties
here are exhaustive answers and examples

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question