Crooker
Crooker

Reputation: 485

How to Update marker icon's source in Leaflet

I'm creating x markers on map in leaflet. (Generating via php script, but it shouldn't be important) Problem is, that I generate new image every 60 seconds, but icons on web maps are not updating.

I want to update icons every X seconds - not important again. I couldn't find any methods to update icons. So I tried removing markers and them recreating them, but wasn't successful too - marker is not defined. Is there any way how to do it?

Edited code:

var icon = L.Icon.extend({
    options: {
        iconSize: [35, 35]
    }
});

function createIcons() {
    //Icons_start
    kopanky = new icon({iconUrl: 'img/kopanky.png'});
    kopankyM.setIcon(kopanky);
    kopankyM.update();
}

function testCreate() {
    kopanky = new icon({iconUrl: 'img/hvezdarna.png'});
    kopankyM.setIcon(kopanky);
    kopankyM.update();
    console.log("test");
}
function createMarkers() {
    //Markers_start
    kopankyM = L.marker([48.9585,17.791666666667]).bindPopup("Kopanky Bile Karpaty").bindLabel('9 kt, 145°', { noHide: true }).addTo(map);
}

createMarkers();
createIcons();

setInterval(function(){
    testCreate();
}, 5000);

I can't refresh via meta-tag, because when I do it, I lost user's current map settings.

Upvotes: 4

Views: 11749

Answers (1)

tmcw
tmcw

Reputation: 11882

Did you look at the documentation? For a way to update icons, you'll find the appropriately-named method setIcon.

Upvotes: 4

Related Questions