Merged in feat/SW-2181-adapt-map-zooming (pull request #1781)

fix(SW-2181): use fitted bounds to adapt the zooming of the map

* fix(SW-2181): use fitted bounds to adapt the zooming of the map


Approved-by: Michael Zetterberg
This commit is contained in:
Matilda Landström
2025-04-14 07:48:38 +00:00
parent e372b91356
commit 51a0855fc1
3 changed files with 20 additions and 4 deletions

View File

@@ -32,7 +32,7 @@ export default async function DestinationOverviewPage() {
<JumpTo /> <JumpTo />
</div> </div>
<div className={styles.mapContainer}> <div className={styles.mapContainer}>
<Suspense fallback={<SkeletonShimmer width={"100%"} height={"100%"} />}> <Suspense fallback={<SkeletonShimmer width="100%" height="100%" />}>
<OverviewMapContainer /> <OverviewMapContainer />
</Suspense> </Suspense>
</div> </div>
@@ -53,15 +53,15 @@ export function DestinationOverviewPageLoading() {
return ( return (
<> <>
<div className={styles.mapContainer}> <div className={styles.mapContainer}>
<SkeletonShimmer width={"100%"} height={"100%"} /> <SkeletonShimmer width="100%" height="100%" />
</div> </div>
<main className={styles.main}> <main className={styles.main}>
<div className={styles.blocks}> <div className={styles.blocks}>
<SkeletonShimmer width={"100%"} height={"100%"} /> <SkeletonShimmer width="100%" height="100%" />
</div> </div>
</main> </main>
<aside className={styles.hotelsAccordions}> <aside className={styles.hotelsAccordions}>
<SkeletonShimmer width={"100%"} height={"100%"} /> <SkeletonShimmer width="100%" height="100%" />
</aside> </aside>
</> </>
) )

View File

@@ -1,5 +1,7 @@
"use client" "use client"
import { Map, type MapProps, useMap } from "@vis.gl/react-google-maps" import { Map, type MapProps, useMap } from "@vis.gl/react-google-maps"
import { useEffect, useState } from "react"
import { useIntl } from "react-intl" import { useIntl } from "react-intl"
import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon" import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon"
@@ -20,11 +22,13 @@ export default function InteractiveMap({
hotelPins, hotelPins,
mapId, mapId,
closeButton, closeButton,
fitBounds = true,
onTilesLoaded, onTilesLoaded,
onActivePoiChange, onActivePoiChange,
}: InteractiveMapProps) { }: InteractiveMapProps) {
const intl = useIntl() const intl = useIntl()
const map = useMap() const map = useMap()
const [hasFittedBounds, setHasFittedBounds] = useState(false)
const mapOptions: MapProps = { const mapOptions: MapProps = {
defaultZoom: 14, defaultZoom: 14,
@@ -47,6 +51,17 @@ export default function InteractiveMap({
} }
} }
useEffect(() => {
if (map && fitBounds && hotelPins?.length && !hasFittedBounds) {
const bounds = new google.maps.LatLngBounds()
hotelPins.forEach((marker) => {
bounds.extend(marker.coordinates)
})
map.fitBounds(bounds, 100)
setHasFittedBounds(true)
}
}, [map, fitBounds, hotelPins, hasFittedBounds])
return ( return (
<div className={styles.mapContainer}> <div className={styles.mapContainer}>
<Map {...mapOptions} onTilesLoaded={onTilesLoaded}> <Map {...mapOptions} onTilesLoaded={onTilesLoaded}>

View File

@@ -11,6 +11,7 @@ export interface InteractiveMapProps {
hotelPins?: HotelPin[] hotelPins?: HotelPin[]
mapId: string mapId: string
closeButton: ReactElement closeButton: ReactElement
fitBounds?: boolean
onTilesLoaded?: () => void onTilesLoaded?: () => void
onActivePoiChange?: (poi: PointOfInterest["name"] | null) => void onActivePoiChange?: (poi: PointOfInterest["name"] | null) => void
} }