Skip to content

Commit 6b71131

Browse files
committed
DBC22-6116: fixed anchored centering on various cases
1 parent 415e20e commit 6b71131

3 files changed

Lines changed: 38 additions & 11 deletions

File tree

src/frontend/src/Components/map/Map.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export default function DriveBCMap(props) {
204204
const [clickedFeature, setClickedFeature] = useState();
205205
const [staleLinkMessage, setStaleLinkMessage] = useState();
206206
const clickedFeatureRef = useRef();
207-
const updateClickedFeature = feature => {
207+
const updateClickedFeature = (feature, center=true) => {
208208
// Remove highlight from feature on click
209209
if (feature && feature instanceof Feature && feature.get('highlight')) {
210210
// Remove highlight from feature
@@ -218,7 +218,10 @@ export default function DriveBCMap(props) {
218218

219219
clickedFeatureRef.current = feature;
220220
setClickedFeature(feature);
221-
updatePosition(feature);
221+
222+
if (center) {
223+
updatePosition(feature);
224+
}
222225
};
223226

224227
const handleSetShowRouteObjs = (value) => {
@@ -373,11 +376,24 @@ export default function DriveBCMap(props) {
373376
geometry = feature.getProperties().altFeature.getGeometry(); // use the point feature's geometry
374377
}
375378

376-
if (mousePointXClicked < 390) {
379+
// Center if panel from bottom or clicked within 390px from left of the screen
380+
if (mousePointXClicked < 390 || smallScreen) {
377381
const zoom = mapView.current.getZoom();
378382
const coords = geometry.flatCoordinates;
379-
if (smallScreen) {
380-
setZoomPanAnchored(mapRef, mapView, zoom, coords);
383+
const mapWidth = mapRef.current?.getSize()?.[0] ?? 0;
384+
385+
// Use anchored pan if panel from bottom or screen smaller than 1000px
386+
const shouldUseAnchoredPan = mapWidth < 1000 || smallScreen;
387+
388+
// Center on top half of the screen, if panel open from bottom
389+
const anchorYFraction = !viewportLargeScreen || isCamDetail ? 0.25 : 0.5;
390+
391+
// Center on right side of screen minus 390px panel, if panel open from left
392+
const anchorXFraction = anchorYFraction !== 0.25 ? (((mapWidth-390)/2) + 390) / mapWidth : 0.5;
393+
394+
if (shouldUseAnchoredPan) {
395+
setZoomPanAnchored(mapRef, mapView, zoom, coords, anchorXFraction, anchorYFraction);
396+
381397
} else {
382398
setZoomPan(mapView, zoom, coords);
383399
}

src/frontend/src/Components/map/handlers/click.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,16 @@ const camClickHandler = (
233233
feature.set('clicked', true, true);
234234
feature.set('unread', false);
235235

236-
updateClickedFeature(feature);
237-
238236
if (isCamDetail) {
237+
// Do not auto center on feature on cam details page
238+
updateClickedFeature(feature, false);
239+
239240
if (feature.get('focusCamera')) {
240241
const zoom = feature.get('zoom');
241242
const pan = feature.get('pan');
242243

243244
const nearbyCount = getVisibleNearbyObjectsCount(mapContext, feature);
245+
244246
setZoomPan(
245247
mapView,
246248
zoom ? zoom : getDefaultZoom(nearbyCount),
@@ -255,6 +257,9 @@ const camClickHandler = (
255257
}
256258

257259
updateReferenceFeature(feature);
260+
261+
} else {
262+
updateClickedFeature(feature);
258263
}
259264
};
260265

src/frontend/src/Components/map/helpers/map.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,15 @@ export const setZoomPan = (mapView, zoom, panCoords) => {
6565
mapView.current.animate(args);
6666
};
6767

68-
// Same as setZoomPan but places `coordinate` at a pixel anchor (default: horizontal center,
69-
// upper quarter) so the point stays visible above a bottom sheet on mobile.
70-
export const setZoomPanAnchored = (mapRef, mapView, zoom, coordinate, anchorYFraction = 0.25) => {
68+
// Used when we want to center on somewhere that's not the actual center
69+
export const setZoomPanAnchored = (
70+
mapRef,
71+
mapView,
72+
zoom,
73+
coordinate,
74+
anchorXFraction,
75+
anchorYFraction
76+
) => {
7177
if (!mapView.current || !mapRef.current) {
7278
return;
7379
}
@@ -79,7 +85,7 @@ export const setZoomPanAnchored = (mapRef, mapView, zoom, coordinate, anchorYFra
7985
}
8086

8187
const view = mapView.current;
82-
const position = [size[0] / 2, size[1] * anchorYFraction];
88+
const position = [size[0] * anchorXFraction, size[1] * anchorYFraction];
8389
const prevCenter = view.getCenter().slice();
8490
view.centerOn(coordinate, size, position);
8591
const targetCenter = view.getCenter().slice();

0 commit comments

Comments
 (0)