I
I
iziakatzman2015-04-28 20:44:42
Yandex
iziakatzman, 2015-04-28 20:44:42

How to change the placemark icon in the markers already placed on the map?

The point is this. I have an ajax request every 10 seconds, which pulls out the coordinate of the current location of the object from the database. All the coordinates that I pull out I store in myCollection of type GeoObjectCollection . Here is part of the code:

myPlacemark = new ymaps.Placemark([data.lat, data.lon], { 
                        content: counter, 
                        hintContent: 'Собственный значок метки'
                    }, {
                        iconLayout: 'default#image', // тип макета
                       //iconImageHref: 'http://api.yandex.ru/maps/doc/jsapi/2.x/examples/images/myIcon.gif',
                        //iconImageSize: [30, 42], // размеры метки
                        //iconImageOffset: [-3, -42]
                    });

myCollection.add(myPlacemark);
myMap.geoObjects.add(myCollection);

Well, I also wanted the standard marker to be only at the current position of the object, i.e. when I extract the coordinates of a new point and display it on the map, all previous markers should change their icons. What I see now is the way out: before displaying the previous point, clear myCollection and draw them again, but with different icons, and then again change the url of the icons for the current position. If there is a better way to do this, please share it.
The disadvantage of clearing the entire myCollection is that you will have to create some kind of spare array with coordinates in order to then restore a new collection from it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
forgotten, 2015-04-29
@forgotten

For example, to have two collections - one for the last icon, the second for the rest. When adding a new icon, the previous one is first transferred to another collection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question