feat(web): auto fit bounds for map modal (#20345)

This commit is contained in:
Dag Stuan 2025-07-28 18:36:37 +02:00 committed by GitHub
parent d34670bae6
commit 66ea75072d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 13 deletions

View File

@ -56,6 +56,7 @@
popup?: import('svelte').Snippet<[{ marker: MapMarkerResponseDto }]>; popup?: import('svelte').Snippet<[{ marker: MapMarkerResponseDto }]>;
rounded?: boolean; rounded?: boolean;
showSimpleControls?: boolean; showSimpleControls?: boolean;
autoFitBounds?: boolean;
} }
let { let {
@ -73,9 +74,21 @@
popup, popup,
rounded = false, rounded = false,
showSimpleControls = true, showSimpleControls = true,
autoFitBounds = true,
}: Props = $props(); }: Props = $props();
const initialCenter = center; // Calculate initial bounds from markers once during initialization
const initialBounds = (() => {
if (!autoFitBounds || center || zoom !== undefined || !mapMarkers || mapMarkers.length === 0) {
return undefined;
}
const bounds = new maplibregl.LngLatBounds();
for (const marker of mapMarkers) {
bounds.extend([marker.lon, marker.lat]);
}
return bounds;
})();
let map: maplibregl.Map | undefined = $state(); let map: maplibregl.Map | undefined = $state();
let marker: maplibregl.Marker | null = null; let marker: maplibregl.Marker | null = null;
@ -277,7 +290,9 @@
style="" style=""
class="h-full {rounded ? 'rounded-2xl' : 'rounded-none'}" class="h-full {rounded ? 'rounded-2xl' : 'rounded-none'}"
{zoom} {zoom}
center={initialCenter} {center}
bounds={initialBounds}
fitBoundsOptions={{ padding: 50, maxZoom: 15 }}
attributionControl={false} attributionControl={false}
diffStyleUpdates={true} diffStyleUpdates={true}
onload={(event) => { onload={(event) => {

View File

@ -8,10 +8,9 @@
type Props = { type Props = {
onClose: (assetIds?: string[]) => void; onClose: (assetIds?: string[]) => void;
mapMarkers: MapMarkerResponseDto[]; mapMarkers: MapMarkerResponseDto[];
zoom?: number;
}; };
let { onClose, mapMarkers, zoom }: Props = $props(); let { onClose, mapMarkers }: Props = $props();
</script> </script>
<Modal title={$t('map')} size="giant" {onClose}> <Modal title={$t('map')} size="giant" {onClose}>
@ -26,15 +25,7 @@
</div> </div>
{/await} {/await}
{:then { default: Map }} {:then { default: Map }}
<Map <Map clickable={false} {mapMarkers} onSelect={onClose} showSettings={false} rounded autoFitBounds />
center={undefined}
{zoom}
clickable={false}
{mapMarkers}
onSelect={onClose}
showSettings={false}
rounded
/>
{/await} {/await}
</div> </div>
</div> </div>