4

Does anyone have a method for adding a title to a leaflet layers control? Just a line of text, for example "Available layers." Ideally I'd like to add a link to the text as well.

I thought it would be simple, but I haven't been able to find a solution. I tried methods similar to this question (radio button/checkbox remains after adding "dummy" layer) and this question (adds div to end of layers, seems more complex than my needs). Unfortunately with my experience level, I haven't been able to connect the dots. Any suggestions?

5 Answers 5

7

Input Elements in Layer Control are present under

.leaflet-control-layers-overlays

$(".leaflet-control-layers-overlays").prepend("<label>Available layers</label>"); You can also assign a class and add some styling on this. This is not good solution but hope it helps you.

3

#Leaflet1.7
Here is the vanilla JS minimal solution.

// 0. Your's WMS layers:      
var fooLayers = {
      "Aaa" : ign_ari,
      "Bbb" : ign_cri,
      "Ccc" : ign_grvi
};

// 1. start with Control
var fooLegend = L.control({position: 'topleft'});

fooLegend.onAdd = function () {
      var div = L.DomUtil.create('div');
      // here is Your part:
      div.innerHTML = '<span class='your-class'>Your Title Text</span>';
      return div;
};
fooLegend.addTo(map);

var fooCtrl = L.control.layers(fooLayers, null,
              {collapsed : false, position: 'topleft'})
              .addTo(map);

//
// Nothing unusual, until now:
var fooCtrlDiv = fooCtrl.getContainer();
fooCtrlDiv.insertBefore(fooLegend.getContainer(), fooCtrlDiv.firstChild);
1
  • This worked, thank you. Looking for a solution where it works with collapsed: true now :) Commented Feb 17, 2022 at 9:31
2

Leaflet 1.7: Addition to @Mruk's answer: If you want to use collapsed: true, then you have to dig a little bit deeper in the DOM tree:

const fooCtrlDiv = fooCtrl.getContainer();

fooCtrlDiv
  .querySelector('.leaflet-control-layers-list')
  .insertBefore(
    fooLegend.getContainer(),
    layerControlDiv.querySelector('.leaflet-control-layers-list').firstChild
  );
1

You can assign a title attribute to the control. Here's an obviously incomplete snippet from a current project:

// Add settings button
var atlasMapSettings = L.Control.extend({
  options: {
    position: 'topleft' 
  },
  onAdd: function (map) {
    var control = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom atlas-settings-control closed');
    var icon    = L.DomUtil.create('div', 'fa fa-gear closed', control);
    var content = L.DomUtil.create('div', 'control-content empty', control);

    $(icon).attr('title', 'Map settings');
2
  • It looks like the braces aren't matching up? I'm not sure where the first one should close.
    – Evan
    Commented Nov 29, 2017 at 23:52
  • It's a partial example that depends on extending a control object, not just adding a basic control. (Which I realize is much more complicated, and maybe not super helpful here, on second thought.) More about custom controls: odoe.net/blog/custom-leaflet-control
    – SteveM
    Commented Dec 8, 2017 at 20:23
0

Leaflet 1.9.4 & Angular

const baseLayers: LayersObject = {};
const overlays: LayersObject = {};
const layerControl: Control.Layers = control.layers(baseLayers, overlays);
layerControl.addTo(this.map);

// Important: getContainer only returns a value when called after addTo(map)
const container: HTMLElement | undefined = layerControl.getContainer();
if (!container) return;
const h5 = document.createElement('h5');
h5.textContent = 'My Title';
container.prepend(h5);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.