Estoy tratando de recrear el ejemplo de alternancia de Mapbox GL JS aquí: https://docs.mapbox.com/mapbox-gl-js/example/toggle-layers/
En el ejemplo, cuando hace clic en el botón, cambia inmediatamente su estado a no visible y apaga la capa. En mi código, primero debe hacer clic en el botón dos veces para que cambie a no visible y apague la capa y luego el botón se comporte normalmente. ¿Alguna sugerencia sobre cómo obtenerlo para que solo tenga que hacer clic una vez inicialmente? Esta es la sección del código que creo que es problemática:
for (const id of toggleableLayerIds) { // Skip layers that already have a button set up. if (document.getElementById(id)) { continue; } // Create a link. const link = document.createElement('a'); link.id = id; link.href = '#'; link.textContent = id; link.className = 'active'; // Show or hide layer when the toggle is clicked. link.onclick = function (e) { const clickedLayer = this.textContent; e.preventDefault(); e.stopPropagation(); const visibility = map.getLayoutProperty( clickedLayer, 'visibility' );