From 573868eccbea0e3cdda683abbfe1125351642ed7 Mon Sep 17 00:00:00 2001 From: Ramin Date: Tue, 2 Jun 2026 08:54:56 +0200 Subject: [PATCH] feat(ui): zoom map to saved area when editing a job (#313) Fit the job edit map to existing polygon areas on init. --- ui/src/components/map/Map.jsx | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ui/src/components/map/Map.jsx b/ui/src/components/map/Map.jsx index 9a6cd8a..7725856 100644 --- a/ui/src/components/map/Map.jsx +++ b/ui/src/components/map/Map.jsx @@ -8,6 +8,7 @@ import maplibregl from 'maplibre-gl'; import 'maplibre-gl/dist/maplibre-gl.css'; import '@mapbox/mapbox-gl-draw/dist/mapbox-gl-draw.css'; import { fixMapboxDrawCompatibility, addDrawingControl, setupAreaFilterEventListeners } from './MapDrawingExtension.js'; +import { getBoundsFromCoords } from '../../views/listings/mapUtils.js'; import './Map.less'; export const GERMANY_BOUNDS = [ @@ -66,6 +67,7 @@ export default function Map({ const mapContainerRef = useRef(null); const mapRef = useRef(null); const drawRef = useRef(null); + const hasFittedToInitialAreaRef = useRef(false); // Initialize map - ONLY when container changes, never reinitialize useEffect(() => { @@ -128,6 +130,17 @@ export default function Map({ } catch (error) { console.error('Error loading spatial filter:', error); } + + if (!hasFittedToInitialAreaRef.current) { + const coords = initialSpatialFilter.features.flatMap((feature) => + feature.geometry?.type === 'Polygon' ? feature.geometry.coordinates.flat() : [], + ); + const bounds = getBoundsFromCoords(coords); + if (bounds) { + mapRef.current.fitBounds(bounds, { padding: 50, maxZoom: 15, duration: 0 }); + hasFittedToInitialAreaRef.current = true; + } + } } // Setup drawing event listeners