I have a map that shows various geoJson layers of public transit routes in Auckland, NZ. I have added some buttons to the page, independent from the leaflet map, and I only want to have one selected, or to remove the previous geoJson layer when a different button is clicked
I have tried various solutions, so far I have this working:
HTML:
<button class='btn' id="busButton">Bus Services</button>
<button class='btn' id="cycleButton">Cycle Lanes</button>
<button class='btn' id="ferryButton">Ferry Routes</button>
Script:
$("#busButton").click(function(event) {
event.preventDefault();
if(map.hasLayer(busRoutesLayer)) {
$(this).removeClass('selected');
map.removeLayer(busRoutesLayer);
} else {
map.addLayer(busRoutesLayer);
$(this).addClass('selected');
}
});
$("#cycleButton").click(function(event) {
event.preventDefault();
if(map.hasLayer(cycleRouteLayer)) {
$(this).removeClass('selected');
map.removeLayer(cycleRouteLayer);
} else {
map.addLayer(cycleRouteLayer);
$(this).addClass('selected');
}
});
So far this lets me select multiple layers, but I do not know how to set up a function to remove a layer when one is already on the map, or make the other buttons unclickable until the layer is turned off. Any help appreciated.