jjimenezshaw / Leaflet.Control.Layers.Tree

a Tree Layers Control for Leaflet
https://jjimenezshaw.github.io/Leaflet.Control.Layers.Tree/examples/basic.html
BSD 3-Clause "New" or "Revised" License
146 stars 36 forks source link

Label Reference of Overlays for overlayadd and overlayremove to implement legend control #58

Closed cspence001 closed 2 years ago

cspence001 commented 2 years ago

My previous code read in the eventLayer.name to trigger legends on/off in select/deselection, however using L.control.layers.tree I can't read in the label references of the overlay layers in the map.on event handler (overlayadd, overlayremove). How does the plug-in reference the label for the layers?

jjimenezshaw commented 2 years ago

I do not know if I understand your question. An easy example would be useful. But probably the solution goes using stamp (https://leafletjs.com/reference.html#util-stamp) to uniquely identify the layers.

cspence001 commented 2 years ago

example :

var overlaysTree = {
    label: 'Points Selection',
    selectAllCheckbox: 'Un/select all',
    children: [
      {
        label: '<b>County Pleth</b>',
        selectAllCheckbox: true,
        children: [
          { label: 'Current Approval', layer: pleth },
          { label: 'Jobs Reported', layer: jobspleth },
                ]
            }, 
      {
        label: '<b>Block Group Pleth<b>',
        selectAllCheckbox: true,
        children: [
          { label: 'Current Approval', layer: plethblockgroup },
          { label: 'Jobs Reported', layer: jobsblockgroup },
                ]
            }
            ]
        }

        myMap.on('overlayadd', function(eventLayer) {
            if (eventLayer.name === '<b>County Pleth</b>') {
              legend.addTo(this);
            } 
            if (eventLayer.name === '<b>Block Group Pleth</b>') {
              blocklegend.addTo(this);
            }
        }
        )

        myMap.on('overlayremove', function(eventLayer) {
            if (eventLayer.name === '<b>County Pleth</b>') {
              this.removeControl(legend);
            } 
            if (eventLayer.name === '<b>Block Group Pleth</b>') {
              this.removeControl(blocklegend);
            }
        }
        )
jjimenezshaw commented 2 years ago

Something like this could be my solution. (See that Country Pleth is not a single layer, but it includes two.)

if (L.stamp(eventLayer) === L.stamp(pleth) || L.stamp(eventLayer) === L.stamp(jobspleth)) {
...
cspence001 commented 2 years ago

In that case by reference the label names I used in my previous code that enabled my legend control would be accessible by the following labels:

var overlaysTree = {
    label: 'Points Selection',
    selectAllCheckbox: 'Un/select all',
    children: [
      {
        label: '<b>County Pleth</b>',
        selectAllCheckbox: true,
        children: [
          { label: 'Current Approval A', layer: pleth },
          { label: 'Jobs Reported', layer: jobspleth },
                ]
            }, 
      {
        label: '<b>Block Group Pleth<b>',
        selectAllCheckbox: true,
        children: [
          { label: 'Current Approval B', layer: plethblockgroup },
          { label: 'Jobs Reported', layer: jobsblockgroup },
                ]
            }
            ]
        }

        myMap.on('overlayadd', function(eventLayer) {
            if (eventLayer.name === 'Current Approval A') {
              legend.addTo(this);
            } 
            if (eventLayer.name === 'Current Approval B') {
              blocklegend.addTo(this);
            }
        }
        )

        myMap.on('overlayremove', function(eventLayer) {
            if (eventLayer.name === 'Current Approval A') {
              this.removeControl(legend);
            } 
            if (eventLayer.name === 'Current Approval B') {
              this.removeControl(blocklegend);
            }
        }
        )

I tried the L.stamp solution but still isn't reading in the legend for the layer reference.

cspence001 commented 2 years ago
   myMap.on('overlayadd', function(eventLayer) {
        if (myMap.hasLayer(pleth)) {
          legend.addTo(this);
        } 
        if (myMap.hasLayer(jobspleth)) {
          blocklegend.addTo(this);
        }
    }