Skip to content

Commit b7bf7ce

Browse files
fix(map): preserve map view on entering drawing mode
When activating the drawing mode, the map would zoom out to the globe level instead of maintaining the current zoom and position. This was caused by the `setupDrawingTools` function not restoring the map's view state after adding the MapboxDraw control. This commit fixes the issue by explicitly restoring the map's center, zoom, and pitch from a stored reference immediately after the drawing control is added. A `flyTo` with a duration of 0 is used to make the transition seamless for the user. This is wrapped in a `setTimeout` to ensure the view restoration happens after any asynchronous initialization of the MapboxDraw control, addressing a potential timing issue.
1 parent cba65dc commit b7bf7ce

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

components/map/mapbox-map.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,18 @@ export const Mapbox: React.FC<{ position?: { latitude: number; longitude: number
269269
// Add control to map
270270
map.current.addControl(drawRef.current, 'top-right')
271271

272-
// Restore the map's view state
273-
const { center, zoom, pitch } = currentMapCenterRef.current
274-
map.current.flyTo({
275-
center,
276-
zoom,
277-
pitch,
278-
duration: 0 // Fly instantly
279-
})
272+
// Restore the map's view state after a brief delay to ensure it's not overridden
273+
setTimeout(() => {
274+
if (map.current) {
275+
const { center, zoom, pitch } = currentMapCenterRef.current
276+
map.current.flyTo({
277+
center,
278+
zoom,
279+
pitch,
280+
duration: 0 // Fly instantly
281+
})
282+
}
283+
}, 0)
280284

281285
// Set up event listeners for measurements
282286
map.current.on('draw.create', updateMeasurementLabels)

0 commit comments

Comments
 (0)