Supporting Links
Website Location:
http://bit.ly/2fXCiTH
.JS File Location:
http://bit.ly/2g56W0U
Trying to accomplish
I would like to have a specific .png legend associated to each individual layer.
Problems so far
I have not been able to get code to recognize the layers being fired
My non-working code so far
var temperaturesLegend = L.control({
position: 'bottomright'
});
var xwLegend = L.control({
position: 'bottomright'
});
temperaturesLegend.onAdd = function(map) {
var div = L.DomUtil.create('div', 'info legend');
div.innerHTML +=
'';
return div;
};
//temperaturesLegend.addTo(map);
xwLegend.onAdd = function(map) {
var div = L.DomUtil.create('div', 'info legend');
div.innerHTML +=
'';
return div;
};
// count plus 1
others = 1;
// Add and remove layers
map.on('overlayadd', function(eventLayer) {
// Switch to the temperaturesLegend...
if (eventLayer.name === 'temperatures') {
// xwlegend.removeFrom(map);
temperaturesLegend.addTo(map);
} else { // Or switch to the xwlegend...
if (others < 1) {
xwLegend.addTo(map);
}
others++; // temperaturesLegend removeFrom(map);
//xwlegend.addTo(map);
}
});
// Add and remove layers
map.on('overlayremove', function(eventLayer) {
temperatures = eventLayer;
//console.log(eventLayer.name);
// Switch to the xwlegend...
if (eventLayer.name === 'temperatures') {
this.removeControl(temperaturesLegend);
} else { // Or switch to the xwlegend...
others--;
if (others < 1) {
this.removeControl(xwLegend);
}
}
});
the problem i ran into with the code above is two fold. The way I was testing was to basically have the first legend "temperaturesLegend" show under the default map/layer and the have the "xwLegend" show up if you selected any other layer. (1) I could not get the temperaturesLegend to come back after selecting another layer and (2) the xwLegend would not replace itself, it would continue to add. So if you went to one layer then another you would have two instances of the legend side by side.
– John Daniel Jarvis Oct 11 '17 at 00:48