diff --git a/frontend/src/pages/TourNavigationPage.tsx b/frontend/src/pages/TourNavigationPage.tsx index b0de235..8f31015 100644 --- a/frontend/src/pages/TourNavigationPage.tsx +++ b/frontend/src/pages/TourNavigationPage.tsx @@ -16,13 +16,13 @@ interface TourNavigationPageProps { onBack: () => void; } -const FitBounds = ({ coords }: { coords: [number, number][] }) => { +const FitBounds = ({ coords, userInteractedRef }: { coords: [number, number][], userInteractedRef: React.MutableRefObject }) => { const map = useMap(); useEffect(() => { - if (coords.length > 0) { + if (coords.length > 0 && !userInteractedRef.current) { map.fitBounds(coords, { padding: [60, 60], maxZoom: 15 }); } - }, [map, coords]); + }, [map, coords, userInteractedRef]); return null; }; @@ -69,14 +69,24 @@ const CompassInteractionDetector = ({ return null; }; -const MapSizeHandler = () => { +const MapSizeHandler = ({ mapRef, userInteractedRef }: { mapRef: React.MutableRefObject, userInteractedRef: React.MutableRefObject }) => { const map = useMap(); useEffect(() => { const timer = setTimeout(() => { - map.invalidateSize(); + if (!mapRef.current) return; + + const hadUserInteracted = userInteractedRef.current; + const currentCenter = map.getCenter(); + const currentZoom = map.getZoom(); + + mapRef.current.invalidateSize(); + + if (hadUserInteracted) { + mapRef.current.setView(currentCenter, currentZoom, { animate: false }); + } }, 100); return () => clearTimeout(timer); - }, [map]); + }, [map, mapRef, userInteractedRef]); return null; }; @@ -85,12 +95,28 @@ export const TourNavigationPage: React.FC = ({ tourId, const [currentHeading, setCurrentHeading] = useState(0); const [routeGeometry, setRouteGeometry] = useState<[number, number][] | null>(null); const [error, setError] = useState(null); - const [mapReady, setMapReady] = useState(false); + const userInteractedRef = useRef(false); const mapRef = useRef(null); const watchIdRef = useRef(null); const fetchLockRef = useRef(false); + useEffect(() => { + const map = mapRef.current; + if (!map) return; + + const onZoomEnd = () => { userInteractedRef.current = true; }; + const onMoveEnd = () => { userInteractedRef.current = true; }; + + map.on('zoomend', onZoomEnd); + map.on('moveend', onMoveEnd); + + return () => { + map.off('zoomend', onZoomEnd); + map.off('moveend', onMoveEnd); + }; + }, [mapRef.current]); + const originLat = routeData?.origin?.lat; const originLng = routeData?.origin?.lng; const destLat = routeData?.destination?.lat; @@ -234,8 +260,9 @@ export const TourNavigationPage: React.FC = ({ tourId, minZoom={2} > - + + {routeData.origin && ( @@ -250,7 +277,6 @@ export const TourNavigationPage: React.FC = ({ tourId, )} - {routeGeometry && } {routeGeometry && ( )}