Z
Z
zlodiak2018-07-28 13:00:59
JavaScript
zlodiak, 2018-07-28 13:00:59

How to loop through a collection of markers?

There is a map. You can place markers on it with the left mouse button. These markers are stored in localStorage. Each time the map is loaded, markers are taken from localStorage and applied to the map.
JSFILLDE here.
I put a click handler on the marker. When clicked, it turns green. But I would like that before this operation, iterate over all existing markers and paint them blue. Here is a piece of problematic code where I can't iterate over and recolor all the markers:

collectionMarkers.events.add('click', function(e) {
        var target = e.get('target');
        var id = target.properties.get('id');
        console.log(id, target);   
        selectedMarker = id;
        console.log('selectedMarker', selectedMarker);       
         
        var markers = myMap.geoObjects.each((geoObject) => {
            console.log(geoObject.properties);
            console.log(geoObject.properties.get('name'));
            console.log(geoObject.properties.get('id'));
            geoObject.options.set({
                preset: 'islands#blueIcon',
            });
        });
        e.get('target').options.set('preset', 'islands#greenIcon');    
    });

As a result, I would like to change the color of the clicked marker to green. help me please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-07-28
@zlodiak

So ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question