最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change leaflet layer control icons - Stack Overflow

programmeradmin4浏览0评论

How can I change the icon for the Leaflet layers control icon in my webpage, when there are more than one such layers control?

The goal is to have different icons here:

The code for that looks like:

var layers1 = L.control.layers(...).addTo(map);
var layers2 = L.control.layers(...).addTo(map);

If I change the control's icon via CSS, it changes in all layers controls.

How can I change the icon for the Leaflet layers control icon in my webpage, when there are more than one such layers control?

The goal is to have different icons here:

The code for that looks like:

var layers1 = L.control.layers(...).addTo(map);
var layers2 = L.control.layers(...).addTo(map);

If I change the control's icon via CSS, it changes in all layers controls.

Share Improve this question edited May 28, 2018 at 8:44 IvanSanchez 19.1k3 gold badges37 silver badges49 bronze badges asked May 26, 2018 at 15:18 Rickyc81Rickyc81 331 gold badge1 silver badge7 bronze badges 2
  • 1 There are multiple ways to do this, however would be nice to see what you've done so far so we can just point you in the right direction. – fixatd Commented May 26, 2018 at 15:20
  • this is my actual project 80.211.9.217/webgis I would like to replace the icon of the second layer control – Rickyc81 Commented May 26, 2018 at 15:32
Add a ment  | 

2 Answers 2

Reset to default 10

I had the same problem and I found a much faster solution. I simply overrided the background-image attribute of the specific Layer Control from the styles.css of my app in this way:

.leaflet-top.leaflet-right .leaflet-control-layers:nth-child(3) .leaflet-control-layers-toggle {
    background-image: /*set you value*/
}

In my case the second Layer Control was the third child (Toolbar, Layer Control 1 and Layer Control 2), so change that index to get the right Control. Hope it helps!

<Edit 2020: fixed typo in code>

On one hand, I suggest you have a look at the Leaflet list of plugins for layer switchers. There might be one that fits your use case better than two default L.Control.Layers with different icons.


On the other hand: The layers control icon is supplied by CSS, in this line:

.leaflet-control-layers-toggle {
    background-image: url(images/layers.png);
    width: 36px;
    height: 36px;
}

That CSS class name es from the _initLayout private method of L.Control.Layers:

_initLayout: function () {
    var className = 'leaflet-control-layers',
    // [snip]
    var link = this._layersLink = DomUtil.create('a', className + '-toggle', container);        
    // [snip]
}

Note how the toggler HTMLElement, which holds the image, is stored as a private property this._layersLink. That HTMLElement can be manipulated afterwards.

With this knowledge, we can create a subclass of L.Control.Layers, like:

L.Control.Layers.TogglerIcon = L.Control.Layers.extend({
    options: {
        // Optional base CSS class name for the toggler element
        togglerClassName: undefined
    },

    _initLayout: function(){
        L.Control.Layers.prototype._initLayout.call(this);
        if (this.options.togglerClassName) {
            L.DomUtil.addClass(this._layersLink, togglerClassName);
        }
    }
});

And then create two of these layers controls passing that new option, e.g.:

var layers1 =
   new L.Control.Layers.TogglerIcon(..., ..., {togglerClassName: 'layers-flowers'});
var layers2 = 
   new L.Control.Layers.TogglerIcon(..., ..., {togglerClassName: 'layers-cars'});

And then have some CSS for their icons, e.g.:

.layers-flowers {
    background-image: url(images/layers-flowers.png);
    width: 36px;
    height: 36px;
}
.layers-cars {
    /* idem */
}

Please note that instead of adding a CSS class, one can also modify the HTMLElement's inside the code, e.g.:

_initLayout: function(){
    L.Control.Layers.prototype._initLayout.call(this);
    if (this.options.backgroundImageUrl) {
        this._layersLink.style.backgroundImage = this.options.backgroundImageUrl;
    }
}

Albeit that might need a bit more precise fiddling.

发布评论

评论列表(0)

  1. 暂无评论