Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

front: enhance viewport computation function #10268

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions front/src/common/Map/WarpedMap/core/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,23 @@ export function simplifyFeature(feature: MapGeoJSONFeature): Feature {
};
}

export function computeBBoxViewport(boundingBox: BBox | Position, initialViewport: Viewport) {
export function computeBBoxViewport(
boundingBox: BBox | Position,
initialViewport: Viewport,
opts?: { width?: number; height?: number; padding?: number }
): Viewport {
const [minLng, minLat, maxLng, maxLat] = boundingBox;
const viewportTemp = new WebMercatorViewport({ ...initialViewport, width: 600, height: 400 });
const viewportTemp = new WebMercatorViewport({
...initialViewport,
width: opts?.width || 600,
height: opts?.height || 400,
});
const { longitude, latitude, zoom } = viewportTemp.fitBounds(
[
[minLng, minLat],
[maxLng, maxLat],
],
{ padding: 40 }
{ padding: opts?.padding ?? 40 }
);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,24 @@ const Map = ({
const infraID = useInfraID();
const terrain3DExaggeration = useSelector(getTerrain3DExaggeration);
const { viewport, mapSearchMarker, mapStyle, showOSM, layersSettings } = useSelector(getMap);
const mapRef = useRef<MapRef | null>(null);
const mapContainer = useMemo(() => mapRef.current?.getContainer(), [mapRef.current]);

const pathGeometry = useMemo(
() => geometry || pathProperties?.geometry,
[pathProperties, geometry]
);

const mapViewport = useMemo(
() =>
isReadOnly && pathGeometry ? computeBBoxViewport(bbox(pathGeometry), viewport) : viewport,
[isReadOnly, pathGeometry, viewport]
);
const mapViewport = useMemo(() => {
if (isReadOnly && pathGeometry) {
return computeBBoxViewport(bbox(pathGeometry), viewport, {
width: mapContainer?.clientWidth,
height: mapContainer?.clientHeight,
padding: 60,
});
}
return viewport;
}, [isReadOnly, pathGeometry, viewport, mapContainer]);

const [mapIsLoaded, setMapIsLoaded] = useState(false);

Expand All @@ -112,8 +119,6 @@ const Map = ({
[dispatch]
);

const mapRef = useRef<MapRef | null>(null);

const scaleControlStyle = {
left: 20,
bottom: 20,
Expand Down Expand Up @@ -214,10 +219,14 @@ const Map = ({
type: 'LineString',
};
if (points.coordinates.length > 2) {
const newViewport = computeBBoxViewport(bbox(points), mapViewport);
const newViewport = computeBBoxViewport(bbox(points), mapViewport, {
width: mapContainer?.clientWidth,
height: mapContainer?.clientHeight,
padding: 60,
});
dispatch(updateViewport(newViewport));
}
}, [pathGeometry, simulationPathSteps]);
}, [pathGeometry, simulationPathSteps, mapContainer]);

return (
<>
Expand Down
Loading