Skip to content

Commit 8279da9

Browse files
committed
front: enhance viewport computation function
Adding optional parameters on function `computeBBoxViewport` to take care of the map height/width Related to #10240 & #10144 Signed-off-by: Benoit Simard <contact@bsimard.com>
1 parent 18e4c0d commit 8279da9

File tree

2 files changed

+30
-12
lines changed
  • front/src
    • common/Map/WarpedMap/core
    • modules/trainschedule/components/ManageTrainSchedule

2 files changed

+30
-12
lines changed

front/src/common/Map/WarpedMap/core/helpers.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,23 @@ export function simplifyFeature(feature: MapGeoJSONFeature): Feature {
187187
};
188188
}
189189

190-
export function computeBBoxViewport(boundingBox: BBox | Position, initialViewport: Viewport) {
190+
export function computeBBoxViewport(
191+
boundingBox: BBox | Position,
192+
initialViewport: Viewport,
193+
opts?: { width?: number; height?: number; padding?: number }
194+
): Viewport {
191195
const [minLng, minLat, maxLng, maxLat] = boundingBox;
192-
const viewportTemp = new WebMercatorViewport({ ...initialViewport, width: 600, height: 400 });
196+
const viewportTemp = new WebMercatorViewport({
197+
...initialViewport,
198+
width: opts?.width || 600,
199+
height: opts?.height || 400,
200+
});
193201
const { longitude, latitude, zoom } = viewportTemp.fitBounds(
194202
[
195203
[minLng, minLat],
196204
[maxLng, maxLat],
197205
],
198-
{ padding: 40 }
206+
{ padding: opts?.padding || 40 }
199207
);
200208

201209
return {

front/src/modules/trainschedule/components/ManageTrainSchedule/Map.tsx

+19-9
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,24 @@ const Map = ({
9090
const infraID = useInfraID();
9191
const terrain3DExaggeration = useSelector(getTerrain3DExaggeration);
9292
const { viewport, mapSearchMarker, mapStyle, showOSM, layersSettings } = useSelector(getMap);
93+
const mapRef = useRef<MapRef | null>(null);
9394

9495
const pathGeometry = useMemo(
9596
() => geometry || pathProperties?.geometry,
9697
[pathProperties, geometry]
9798
);
9899

99-
const mapViewport = useMemo(
100-
() =>
101-
isReadOnly && pathGeometry ? computeBBoxViewport(bbox(pathGeometry), viewport) : viewport,
102-
[isReadOnly, pathGeometry, viewport]
103-
);
100+
const mapViewport = useMemo(() => {
101+
if (isReadOnly && pathGeometry) {
102+
const mapContainer = mapRef.current?.getContainer();
103+
return computeBBoxViewport(bbox(pathGeometry), viewport, {
104+
width: mapContainer?.clientWidth,
105+
height: mapContainer?.clientHeight,
106+
padding: 60,
107+
});
108+
}
109+
return viewport;
110+
}, [isReadOnly, pathGeometry, viewport, mapRef]);
104111

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

@@ -112,8 +119,6 @@ const Map = ({
112119
[dispatch]
113120
);
114121

115-
const mapRef = useRef<MapRef | null>(null);
116-
117122
const scaleControlStyle = {
118123
left: 20,
119124
bottom: 20,
@@ -214,10 +219,15 @@ const Map = ({
214219
type: 'LineString',
215220
};
216221
if (points.coordinates.length > 2) {
217-
const newViewport = computeBBoxViewport(bbox(points), mapViewport);
222+
const mapContainer = mapRef.current?.getContainer();
223+
const newViewport = computeBBoxViewport(bbox(points), mapViewport, {
224+
width: mapContainer?.clientWidth,
225+
height: mapContainer?.clientHeight,
226+
padding: 60,
227+
});
218228
dispatch(updateViewport(newViewport));
219229
}
220-
}, [pathGeometry, simulationPathSteps]);
230+
}, [pathGeometry, simulationPathSteps, mapRef]);
221231

222232
return (
223233
<>

0 commit comments

Comments
 (0)