mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 17:41:51 +00:00
fix(map): reveal selected places at street level
Use street-level zoom in both renderers while keeping the selected pin between the map panels. Lift the selected Leaflet marker out of a dense group when needed, and keep a late route fit from overriding the selection. Closes #3306
This commit is contained in:
@@ -6,7 +6,7 @@ import { frameLng } from '../../utils/lngFrame';
|
||||
import type { ViewportPadding } from '../../utils/mapViewport';
|
||||
import { useLngFrame } from './lngFrameContext';
|
||||
import { isUnderChrome } from './mapChromePadding';
|
||||
import { selectedPlaceTarget } from './selectedPlaceTarget';
|
||||
import { PLACE_FOCUS_ZOOM, selectedPlaceTarget } from './selectedPlaceTarget';
|
||||
|
||||
interface SelectionControllerProps {
|
||||
/** False while the traveller locked the map (#2010): a pick changes nothing on it. */
|
||||
@@ -16,6 +16,7 @@ interface SelectionControllerProps {
|
||||
dayPlaces: Place[];
|
||||
selectedPlace: Place | null;
|
||||
paddingBox: ViewportPadding;
|
||||
revealSelectedPlace: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,6 +30,7 @@ export function SelectionController({
|
||||
dayPlaces,
|
||||
selectedPlace,
|
||||
paddingBox,
|
||||
revealSelectedPlace,
|
||||
}: SelectionControllerProps) {
|
||||
const map = useMap();
|
||||
const frame = useLngFrame();
|
||||
@@ -39,12 +41,33 @@ export function SelectionController({
|
||||
const lat = target?.lat ?? null;
|
||||
const lng = target?.lng == null ? null : frameLng(frame, target.lng);
|
||||
|
||||
useEffect(() => {
|
||||
if (!follow || selectedPlaceId == null) return;
|
||||
let frame: number | null = null;
|
||||
const revealAfterMove = () => {
|
||||
if (frame !== null) cancelAnimationFrame(frame);
|
||||
frame = requestAnimationFrame(() => revealSelectedPlace());
|
||||
};
|
||||
map.on('moveend', revealAfterMove);
|
||||
return () => {
|
||||
map.off('moveend', revealAfterMove);
|
||||
if (frame !== null) cancelAnimationFrame(frame);
|
||||
};
|
||||
}, [map, follow, selectedPlaceId, revealSelectedPlace]);
|
||||
|
||||
useEffect(() => {
|
||||
const isNew = selectedPlaceId !== prev.current;
|
||||
prev.current = selectedPlaceId;
|
||||
if (!isNew || !follow || !selectedPlaceId || lat == null || lng == null) return;
|
||||
pannedWith.current = paddingBox;
|
||||
panToPlace(map, [lat, lng], paddingBox);
|
||||
const zoom = map.getZoom();
|
||||
const focusZoom = Math.max(zoom, PLACE_FOCUS_ZOOM);
|
||||
const center = paddedCenter(map, [lat, lng], paddingBox, focusZoom);
|
||||
if (zoom < focusZoom) {
|
||||
map.setView(center, focusZoom, { animate: false });
|
||||
} else {
|
||||
map.panTo(center, { animate: true });
|
||||
}
|
||||
}, [selectedPlaceId, follow, lat, lng, map, paddingBox]);
|
||||
|
||||
// The place card only learns its height after it laid out, and grows again when its
|
||||
@@ -56,7 +79,7 @@ export function SelectionController({
|
||||
if (!follow || !selectedPlaceId || lat == null || lng == null) return;
|
||||
if (typeof map.latLngToContainerPoint !== 'function') return;
|
||||
if (isUnderChrome(map.latLngToContainerPoint([lat, lng]), map.getSize(), paddingBox)) {
|
||||
panToPlace(map, [lat, lng], paddingBox);
|
||||
map.panTo(paddedCenter(map, [lat, lng], paddingBox, map.getZoom()), { animate: true });
|
||||
}
|
||||
}, [paddingBox, follow, selectedPlaceId, lat, lng, map]);
|
||||
|
||||
@@ -64,16 +87,19 @@ export function SelectionController({
|
||||
}
|
||||
|
||||
/**
|
||||
* Pan to a place without changing zoom. The centre is offset by the side panels and the
|
||||
* bottom card, so the pin lands in the middle of the *visible* map area rather than the
|
||||
* geometric centre, where the card would cover it.
|
||||
* Offset the centre at the destination zoom so the pin lands in the middle of the
|
||||
* visible map area, above the bottom card and between the side panels.
|
||||
*/
|
||||
function panToPlace(map: L.Map, latlng: [number, number], padding: ViewportPadding): void {
|
||||
function paddedCenter(
|
||||
map: L.Map,
|
||||
latlng: [number, number],
|
||||
padding: ViewportPadding,
|
||||
zoom: number
|
||||
): L.LatLngExpression {
|
||||
if (typeof map.project !== 'function' || typeof map.unproject !== 'function') {
|
||||
map.panTo(latlng, { animate: true });
|
||||
return;
|
||||
return latlng;
|
||||
}
|
||||
const { top, right, bottom, left } = padding;
|
||||
const point = map.project(latlng).add([(right - left) / 2, (bottom - top) / 2]);
|
||||
map.panTo(map.unproject(point), { animate: true });
|
||||
const point = map.project(latlng, zoom).add([(right - left) / 2, (bottom - top) / 2]);
|
||||
return map.unproject(point, zoom);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user