Show/Hide Layers Using a Dropdown Menu
- 6 replies
- 1773 views
- Translate
I have a base layer that I want to remain visible, but I would like to switch between the rest of my layers by using a dropdown menu. I'm trying to write it such that when one layer is selected, the rest are hidden. This is what I have so far, but when I select anything from the dropdown, nothing changes.
// Define the layer names
var layerNames = ["Emergency Repair", "CSA Visit", "Installation", "Other"];
// Function to toggle layers based on dropdown selection
function toggleLayers() {
var selectedOption = event.target.value;
for (var i = 0; i < layerNames.length; i++) {
var layer = this.getOCGs()[layerNames[i]];
if (layer) {
if (layerNames[i].replace(/\s+/g, '') === selectedOption) {
layer.state = !layer.state;
} else {
layer.state = false;
}
}
}
}
// Add an event listener to the dropdown
this.getField("Dropdown2").setAction("Keystroke", "toggleLayers()");
Apologies if this code looks weird. I've resorted to asking ChatGPT for help.
