I'm creating a map with multiple KML layers, and have been able to get the KMLs to appear, however, implementing a simple checkbox to turn each layer on and off is proving to be a problem. I've tried omnivore, and several other plugin things, but none seem to work properly.
HTML Toggle:
<input type="checkbox" id="toggleScottsdale" onclick="toggleScottsdale();"><label> SCT AIR</label><br>
<input type="checkbox" id="toggleSilo" onclick="toggleSilo();"><label> MES SLO</label>
Example KML Layer (there are 5 in total):
fetch('urltofile1.kml')
.then(res => res.text())
.then(kmltext => {
// Create Scottsdale Overlay
const parser = new DOMParser();
const kml = parser.parseFromString(kmltext, 'text/xml');
const scottsdale = new L.KML(kml);
map.addLayer(scottsdale);
});
fetch('urltofile2.kml')
.then(res => res.text())
.then(kmltext => {
// Create Silo Overlay
const parser = new DOMParser();
const kml = parser.parseFromString(kmltext, 'text/xml');
const scottsdale = new L.KML(kml);
map.addLayer(silo);
});
Toggle Function:
function toggleScottsdale() {
if(map.addLayer(scottsdale)) {
map.removeLayer(scottsdale);
} else {
map.addLayer(scottsdale);
}
};
function toggleSilo() {
if(map.addLayer(silo)) {
map.removeLayer(silo);
} else {
map.addLayer(silo);
}
};
Not only does console return that scottsdale
is undefined, but I am unsure how to replicate a toggle for each layer.