fix custom layer replacement, closes #22

This commit is contained in:
vcoppe
2024-07-24 10:21:37 +02:00
parent d1db934d31
commit bfb812d79a
2 changed files with 36 additions and 10 deletions

View File

@@ -39,6 +39,10 @@
}
function createLayer() {
if (selectedLayerId && $customLayers[selectedLayerId].layerType !== layerType) {
deleteLayer(selectedLayerId);
}
if (typeof maxZoom === 'string') {
maxZoom = parseInt(maxZoom);
}
@@ -106,6 +110,8 @@
$tree.basemaps['custom'][layerId] = true;
return $tree;
});
$currentBasemap = layerId;
} else {
selectedOverlayTree.update(($tree) => {
if (!$tree.overlays.hasOwnProperty('custom')) {
@@ -114,6 +120,16 @@
$tree.overlays['custom'][layerId] = true;
return $tree;
});
if ($map && $map.getSource(layerId)) {
// Reset source when updating an existing layer
if ($map.getLayer(layerId)) {
$map.removeLayer(layerId);
}
$map.removeSource(layerId);
}
$currentOverlays.overlays['custom'][layerId] = true;
}
}
@@ -142,8 +158,13 @@
$selectedBasemapTree.basemaps = tryDeleteLayer($selectedBasemapTree.basemaps, 'custom');
}
} else {
$currentOverlays = tryDeleteLayer($currentOverlays, layerId);
$previousOverlays = tryDeleteLayer($previousOverlays, layerId);
$currentOverlays.overlays['custom'][layerId] = false;
if ($previousOverlays.overlays['custom']) {
$previousOverlays.overlays['custom'] = tryDeleteLayer(
$previousOverlays.overlays['custom'],
layerId
);
}
$selectedOverlayTree.overlays['custom'] = tryDeleteLayer(
$selectedOverlayTree.overlays['custom'],

View File

@@ -30,14 +30,19 @@
opacities
} = settings;
$: if ($map) {
// Set style depending on the current basemap
let basemap = basemaps.hasOwnProperty($currentBasemap)
? basemaps[$currentBasemap]
: $customLayers[$currentBasemap]?.value ?? basemaps[defaultBasemap];
$map.setStyle(basemap, {
diff: false
});
function setStyle() {
if ($map) {
let basemap = basemaps.hasOwnProperty($currentBasemap)
? basemaps[$currentBasemap]
: $customLayers[$currentBasemap]?.value ?? basemaps[defaultBasemap];
$map.setStyle(basemap, {
diff: false
});
}
}
$: if ($map && $currentBasemap) {
setStyle();
}
$: if ($map && $currentOverlays) {