F
F
Finesse2017-09-14 06:13:00
JavaScript
Finesse, 2017-09-14 06:13:00

How to change the balloon text of a label added via the ObjectManager (Yandex Maps API)?

I have a map with many dots on it. The points are added using the ObjectManager :

const map = new ymaps.Map(document.getElementById('map'), {
    center: [42, 54],
    zoom: 6
});
const objectManager = new ymaps.ObjectManager({
    clusterize: true
});
objectManager.add({
    type: 'FeatureCollection',
    features: [
        {
            type: 'Feature',
            id: 1,
            geometry: {
                type: 'Point',
                coordinates: [43, 54]
            },
            properties: {
                balloonContentBody: 'Загрузка...'
            }
        },
        {
            type: 'Feature',
            id: 2,
            geometry: {
                type: 'Point',
                coordinates: [41, 55]
            },
            properties: {
                balloonContentBody: 'Загрузка...'
            }
        }
        // ...
    ]
});
map.geoObjects.add(objectManager);

How can I change the content of the balloon (popup window) of a specific point some time after the map has been created? Application example: when creating a map, an AJAX request starts, and after a while the request ends and information appears that needs to be added to the point balloons. At the same time, you cannot abandon the ObjectManager so as not to sacrifice performance.
I tried this, it only turned out to change the color of the label:
objectManager.objects.setObjectOptions(1, {
    preset: 'islands#redIcon',
    balloonContentBody: 'Загружено (тут полезная информация)',
    properties: {
        balloonContentBody: 'Загружено (тут полезная информация)'
    }
});

Example on CodePen

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Finesse, 2017-09-15
@Finesse

objectManager.objects.getById(1).properties.balloonContentBody = 'Загружено (тут полезная информация)';

Where 1 is the id of the desired point. Be careful, objectManager.objects.getByIdmay return null. Demo on CodePen .
The method has a drawback. The contents of the popup window will not change while it is open. The new text is applied before the balloon opens.

D
d-virt, 2017-09-14
@d-virt

https://tech.yandex.ru/maps/doc/jsapi/2.1/ref/refe...
There is an example in the description of the add method.
I'll quote this example:

var myObjectManager = new ymaps.ObjectManager({ clusterize: true }),
    currentId = 0;

// Добавление единичного объекта.
myObjectManager.add({
    type: 'Feature',
    id: currentId++,
    geometry: {
        type: 'Point',
        coordinates: [56.23, 34.79]
    },
    properties: {
        hintContent: 'Текст всплывающей подсказки',
        balloonContent: 'Содержимое балуна'
    }
});
map.geoObjects.add(objectManager);

Tobish You need to add balloonContent to your property

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question