Skip to content

Commit

Permalink
fix updating overlay opacity
Browse files Browse the repository at this point in the history
  • Loading branch information
vcoppe committed Oct 14, 2024
1 parent 60f24f8 commit 65cbf5e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion website/src/lib/assets/layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ export const overpassTree: LayerTreeType = {
export const defaultBasemap = 'mapboxOutdoors';

// Default overlays used (none)
export const defaultOverlays = {
export const defaultOverlays: LayerTreeType = {
overlays: {
world: {
waymarked_trails: {
Expand Down
21 changes: 12 additions & 9 deletions website/src/lib/components/layer-control/LayerControl.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,21 @@
}
function updateOverlays() {
if ($map && $currentOverlays) {
if ($map && $currentOverlays && $opacities) {
let overlayLayers = getLayers($currentOverlays);
try {
let activeOverlays = $map
.getStyle()
.imports.filter((i) => i.id !== 'basemap' && i.id !== 'overlays');
let toRemove = activeOverlays.filter((i) => !overlayLayers[i.id]);
toRemove.forEach((i) => {
$map.removeImport(i.id);
let activeOverlays = $map.getStyle().imports.reduce((acc, i) => {
if (!['basemap', 'overlays', 'glyphs-and-sprite'].includes(i.id)) {
acc[i.id] = i;
}
return acc;
}, {});
let toRemove = Object.keys(activeOverlays).filter((id) => !overlayLayers[id]);
toRemove.forEach((id) => {
$map.removeImport(id);
});
let toAdd = Object.entries(overlayLayers)
.filter(([id, selected]) => selected && !activeOverlays.some((j) => j.id === id))
.filter(([id, selected]) => selected && !activeOverlays.hasOwnProperty(id))
.map(([id]) => id);
toAdd.forEach((id) => {
addOverlay(id);
Expand All @@ -106,7 +109,7 @@
}
}
$: if ($map && $currentOverlays) {
$: if ($map && $currentOverlays && $opacities) {
updateOverlays();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,16 @@
max={1}
step={0.1}
disabled={$selectedOverlay === undefined}
onValueChange={() => {
onValueChange={(value) => {
if ($selectedOverlay) {
$opacities[$selectedOverlay.value] = $overlayOpacity[0];
if ($map) {
if ($map.getLayer($selectedOverlay.value)) {
$map.removeLayer($selectedOverlay.value);
$currentOverlays = $currentOverlays;
if ($map && isSelected($currentOverlays, $selectedOverlay.value)) {
try {
$map.removeImport($selectedOverlay.value);
} catch (e) {
// No reliable way to check if the map is ready to remove sources and layers
}
}
$opacities[$selectedOverlay.value] = value[0];
}
}}
/>
Expand Down

0 comments on commit 65cbf5e

Please sign in to comment.