Closed cspence001 closed 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.
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);
}
}
)
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)) {
...
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.
myMap.on('overlayadd', function(eventLayer) {
if (myMap.hasLayer(pleth)) {
legend.addTo(this);
}
if (myMap.hasLayer(jobspleth)) {
blocklegend.addTo(this);
}
}
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?