// --- THE "TOGGLE CLUB" SCRIPT (WITH TIMING FIX) ---
// 1. DEFINE THE CLUB MEMBERS
var toggleableLayers = [
layer_PopulationDemographics_3,
layer_PropertiesatRiskofFirein30yearspercentile_4,
layer_PropertiesatRiskofFloodRisk30yearspercentile_5,
layer_ExpectedBuildingLossRatepercentile_6,
layer_AgriculturalLossRatepercentile_7,
layer_PercentUnemployment_8,
layer_PercentofPeople200BelowFederalPovertyLine_9,
layer_PercentofPeople100BelowFederalPovertyLinepercentile_10,
layer_EnergyBurdenpercentile_11,
layer_ExpectedPopulationLossRatepercentile_12,
layer_DOTTravelBarrierScorepercentile_13,
layer_LowLifeExpectancypercentile_14,
layer_DiagnosedDiabetespercentile_15,
layer_CurrentAsthmaAmongAdultspercentile_16,
layer_CoronaryHeartDiseasepercentile_17,
];
// 2. THE LOGIC
map.on('overlayadd', function(e) {
var clickedLayer = e.layer;
// CHECK: Is the layer we just turned on part of the "Toggle Club"?
if (toggleableLayers.includes(clickedLayer)) {
// TIMING FIX: Wait 10ms to let the checkbox finish "checking" itself
setTimeout(function() {
// Now loop through the club and remove the others
toggleableLayers.forEach(function(memberLayer) {
// If this member is currently visible...
// AND it is not the one we just clicked...
if (map.hasLayer(memberLayer) && memberLayer !== clickedLayer) {
// Remove it from the map!
// (This triggers the menu to uncheck the box)
map.removeLayer(memberLayer);
}
});
}, 10); // 10 milliseconds delay
}
});