Skip to content

Commit

Permalink
front: enhance viewport computation function
Browse files Browse the repository at this point in the history
Adding optional parameters on function `computeBBoxViewport` to take
care of the map height/width

Related to #10240 & #10144

Signed-off-by: Benoit Simard <[email protected]>
  • Loading branch information
sim51 committed Jan 9, 2025
1 parent 384dcea commit 0e377b3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
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

0 comments on commit 0e377b3

Please sign in to comment.