diff --git a/src/modules/geoportal/components/map-viewer.tsx b/src/modules/geoportal/components/map-viewer.tsx index ea741c5..77a008c 100644 --- a/src/modules/geoportal/components/map-viewer.tsx +++ b/src/modules/geoportal/components/map-viewer.tsx @@ -53,7 +53,7 @@ const LAYER_IDS = { } as const; /** Basemap tile definitions */ -const BASEMAP_TILES: Record = { +const BASEMAP_TILES: Record = { osm: { tiles: [ "https://a.tile.openstreetmap.org/{z}/{x}/{y}.png", @@ -79,6 +79,7 @@ const BASEMAP_TILES: RecordOpenTopoMap (CC-BY-SA)', tileSize: 256, + maxzoom: 17, }, }; @@ -231,47 +232,7 @@ export const MapViewer = forwardRef( } }, [mapReady, layerVisibility, applyLayerVisibility]); - /* ---- Basemap switching ---- */ - useEffect(() => { - const map = mapRef.current; - if (!map || !mapReady) return; - - const source = map.getSource("basemap") as maplibregl.RasterTileSource | undefined; - if (!source) return; - - const def = BASEMAP_TILES[basemap]; - // Update tiles by re-adding the source - // MapLibre doesn't support changing tiles on existing source, so we rebuild - try { - // Remove all layers that depend on basemap source, then remove source - if (map.getLayer("basemap-tiles")) map.removeLayer("basemap-tiles"); - map.removeSource("basemap"); - - map.addSource("basemap", { - type: "raster", - tiles: def.tiles, - tileSize: def.tileSize, - attribution: def.attribution, - }); - - // Re-add basemap layer at bottom - const firstLayerId = map.getStyle().layers[0]?.id; - map.addLayer( - { - id: "basemap-tiles", - type: "raster", - source: "basemap", - minzoom: 0, - maxzoom: 19, - }, - firstLayerId // insert before first existing layer - ); - } catch { - // Fallback: if anything fails, the map still works - } - }, [basemap, mapReady]); - - /* ---- Map initialization ---- */ + /* ---- Map initialization (recreates on basemap change) ---- */ useEffect(() => { if (!containerRef.current) return; @@ -295,13 +256,13 @@ export const MapViewer = forwardRef( type: "raster", source: "basemap", minzoom: 0, - maxzoom: 19, + maxzoom: initialBasemap.maxzoom ?? 19, }, ], }, center: center ?? DEFAULT_CENTER, zoom: zoom ?? DEFAULT_ZOOM, - maxZoom: 20, + maxZoom: initialBasemap.maxzoom ?? 20, }); mapRef.current = map; @@ -555,7 +516,7 @@ export const MapViewer = forwardRef( setMapReady(false); }; // eslint-disable-next-line react-hooks/exhaustive-deps - }, [resolvedMartinUrl]); + }, [resolvedMartinUrl, basemap]); /* ---- Sync center/zoom prop changes ---- */ useEffect(() => {