V
V
Vika Marmeladka2021-04-16 12:43:41
JavaScript
Vika Marmeladka, 2021-04-16 12:43:41

How to get the id of the clicked Yandex Map label?

I tried to use the event from Yandex "add",

geoObjects.events.add("change", function () {
      geoObjects.properties.get("myID")
});


but this does not help me, I constantly get one error in various forms, but the essence is the same, the object does not have an add method
Uncaught TypeError: Cannot read property 'add' of undefined
My goal is to get properties data from the json object, if there are other solutions, offer, while I thought of just getting the ID and everything else from it

var mapCenter = $('#map').data('center');
    var fileMap = $('#map').data('file');

    ymaps.ready()
        .done(function (ym) {
            var myMap = new ym.Map('map', {
                center: mapCenter,
                zoom: 13,
                controls: []
            });


            $.getJSON(fileMap, function (json) {

                var geoObjects = ym.geoQuery(json)
                    .addToMap(myMap)
                    .applyBoundsToMap(myMap, {
                        checkZoomRange: true

                    })
            });

            myMap.behaviors.disable('scrollZoom');
        });


I get this json:

{
  "type": "FeatureCollection",
  "features": [{
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          55.778674,
          37.641944
        ]
      },
      "properties": {
        "balloonContent": "<div class='balloon__inner'><div class='balloon__inner--title'>VISPRING</div><div class='balloon__inner--address'>Адрес: ул. Алабяна, 10 корпус 2 (м. Сокол)</div></div>",
        "hintContent": "ул. Алабяна, 10 корпус 2 (м. Сокол)",

        "images": "assets/img/main-page/pexels-andre-furtado-1429395.png",
        "nameStore": "Фотосфера",
        "address": "ТЦ Мегаполис. Пр. Андропова, д. 8",
        "workTime": "11:00 – 21:00",
        "number": "+7 (495) 212-10-89",
        "siteStore": "www.fsfera.ru"
      },
      "options": {
        "iconLayout": "default#image",
        "iconImageHref": "assets/img/svg/SVG_map-icons/green_location_small.svg",
        "iconImageSize": [55, 65],
        "iconImageOffset": [-28, -60]
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          55.742661, 37.573752
        ]
      },
      "properties": {
        "balloonContent": "<div class='balloon__inner'><div class='balloon__inner--title'>VISPRING</div><div class='balloon__inner--address'>Адрес: ул. Алабяна, 10 корпус 2 (м. Сокол)</div></div>",
        "hintContent": "ул. Алабяна, 10 корпус 2 (м. Сокол)",

        "images": "assets/img/main-page/pexels-andre-furtado-1429395.png",
        "nameStore": "Фотосфера",
        "address": "ТЦ Мегаполис. Пр. Андропова, д. 8",
        "workTime": "11:00 – 21:00",
        "number": "+7 (495) 212-10-89",
        "siteStore": "www.fsfera.ru"
      },
      "options": {
        "iconLayout": "default#image",
        "iconImageHref": "assets/img/svg/SVG_map-icons/green_location_small.svg",
        "iconImageSize": [55, 65],
        "iconImageOffset": [-28, -60]
      }
    }
  ]}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vika Marmeladka, 2021-04-16
@homsi959

Question solved:
I tried to use the add event directly on geoObjects which, as I understand it, does not have this method. It was necessary to say goodbye through the map and only after to the objects

myMap.geoObjects.events.add('click', function (e) {
    console.log(e.get('target'));
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question