diff --git a/frontend/src/pages/TourNavigationPage.tsx b/frontend/src/pages/TourNavigationPage.tsx index 8f31015..741acb3 100644 --- a/frontend/src/pages/TourNavigationPage.tsx +++ b/frontend/src/pages/TourNavigationPage.tsx @@ -16,13 +16,14 @@ interface TourNavigationPageProps { onBack: () => void; } -const FitBounds = ({ coords, userInteractedRef }: { coords: [number, number][], userInteractedRef: React.MutableRefObject }) => { +const FitBounds = ({ coords, canFitRef, compassActive }: { coords: [number, number][], canFitRef: React.MutableRefObject, compassActive: boolean }) => { const map = useMap(); useEffect(() => { - if (coords.length > 0 && !userInteractedRef.current) { + if (coords.length > 0 && canFitRef.current && !compassActive) { + canFitRef.current = false; map.fitBounds(coords, { padding: [60, 60], maxZoom: 15 }); } - }, [map, coords, userInteractedRef]); + }, [map, coords, canFitRef, compassActive]); return null; }; @@ -69,24 +70,30 @@ const CompassInteractionDetector = ({ return null; }; -const MapSizeHandler = ({ mapRef, userInteractedRef }: { mapRef: React.MutableRefObject, userInteractedRef: React.MutableRefObject }) => { +const MapRefSetter = ({ mapRef, userInteractedRef }: { mapRef: React.MutableRefObject, userInteractedRef: React.MutableRefObject }) => { + const map = useMap(); + useEffect(() => { + mapRef.current = map; + 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); + }; + }, [map, mapRef, userInteractedRef]); + return null; +}; + +const MapSizeHandler = ({ mapRef }: { mapRef: React.MutableRefObject }) => { const map = useMap(); useEffect(() => { const timer = setTimeout(() => { - 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 }); - } + mapRef.current?.invalidateSize(); }, 100); return () => clearTimeout(timer); - }, [map, mapRef, userInteractedRef]); + }, [map, mapRef]); return null; }; @@ -96,27 +103,12 @@ export const TourNavigationPage: React.FC = ({ tourId, const [routeGeometry, setRouteGeometry] = useState<[number, number][] | null>(null); const [error, setError] = useState(null); const userInteractedRef = useRef(false); + const canFitRef = useRef(true); 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; @@ -260,9 +252,9 @@ export const TourNavigationPage: React.FC = ({ tourId, minZoom={2} > - - - + + + {routeData.origin && (