Arumugaguru
Arumugaguru

Reputation: 1

Change Marker icon based on tile layer change - leaflet

I am new to leaflet and I would like to know how to achieve changing the Marker icon based on change in tile layer.

For eg: if I have location icon in base layer and want this to be changed as bus icon when choosing satellite as tile layer.

Upvotes: 0

Views: 768

Answers (1)

nrhode
nrhode

Reputation: 932

The best way is you define your icons and set an event when layer changes:

var icon1 = L.icon({
    iconUrl: 'marker.png',
    iconSize:     [38, 95],
});
var icon2 = L.icon({
    iconUrl: 'bus.png',
    iconSize:     [38, 95],
});

var marker = L.marker([51.5, -0.09], {icon: icon1}).addTo(map);

map.on('baselayerchange', function(e) {
    if(e.layer.name == "whatyouwant"){
        marker.setIcon(icon1);
     }else{
        marker.setIcon(icon2);
     }
});

Upvotes: 1

Related Questions