I'm trying to make it so that the width of the overlay line changes depending on which layer is in use. To wit I have this code:
var params = {
color: 'blue',
opacity: 0.75,
smoothFactor: 1
};
var latlngs = [[30.267222, -97.743056],[31.267222, -96.743056]];
tempParams = Object.assign(params, {weight: 10});
satelliteLines.addLayer(L.polyline(latlngs, tempParams));
tempParams = Object.assign(params, {weight: 3});
lightLines.addLayer(L.polyline(latlngs, tempParams));
tempParams = Object.assign(params, {weight: 5});
streetsLines.addLayer(L.polyline(latlngs, tempParams));
var hasOverlay = false;
mymap.on('baselayerchange', function(e) {
switch (e.name) {
case 'Light':
if (hasOverlay) {
lightLines.addTo(mymap);
streetsLines.removeFrom(mymap);
satelliteLines.removeFrom(mymap);
}
break;
case 'Streets':
if (hasOverlay) {
lightLines.removeFrom(mymap);
streetsLines.addTo(mymap);
satelliteLines.removeFrom(mymap);
}
break;
case 'Satellite':
if (hasOverlay) {
lightLines.removeFrom(mymap);
streetsLines.removeFrom(mymap);
satelliteLines.addTo(mymap);
}
}
});
mymap.on('overlayadd', function(e) {
console.log('overlayadd called');
hasOverlay = true;
});
This works great for when I turn the "Demo" overlay on but it doesn't work so well when I turn the overlay off and I'm in a layer other than the default "Light" layer. What happens, when I'm in a Layer other than the "Light" layer is that the line doesn't get removed. It only gets removed in the "Light" layer.
To overcome that problem I added this:
mymap.on('overlayremove', function(e) {
console.log('overlayremove called');
lightLines.removeFrom(mymap);
streetsLines.removeFrom(mymap);
satelliteLines.removeFrom(mymap);
});
That's when things start to get strange. In order to remove all this "Demo" overlayer line for any layer other than the default "Light" layer I now have to double click the "Demo" overlay link in the overlay control dialog for the "Demo" overlay line to disappear. I want it to disappear after just one click (which is how it'd normally work anyway if I wasn't trying to adjust the width dynamically).
Per the console.log's I see that the first time I change layers overlayremove is called but overlayadd isn't. This suggests to me that lightLines.removeFrom(mymap); is triggering the overlayremove event. But if that's the case why isn't the overlayadd event being called? Regardless, if I uncheck and then check the "Demo" overlay link in the top right nothing happens. At least not the first time I click it. The line is stillthere and neither overlayadd or overlayremove are called. BUT when I click the "Demo" overlay link overlayadd is called. When I uncheck it after that the line disappears, with overlayremove finally being called.
Anyway, I don't understand this behavior.
Any ideas?
I just want to make it so that the variable width overlay line goes away, no matter what layer is being displayed, after the overlay entry in the menu has been unchecked.
Here's a standalone demo: