Skip to content

Commit 0458bbc

Browse files
feat(geoLocation): stop centering map after user pans map
1 parent daf5e07 commit 0458bbc

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

src/plugins/geoLocation/store.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import { detectDeniedGeolocationEarly } from './utils/detectDeniedGeolocationEar
3131
import { getGeoLocationStyle } from './utils/olStyle'
3232
import { positionChanged } from './utils/positionChanged'
3333

34+
let isUserInteracting = false
35+
3436
/* eslint-disable tsdoc/syntax */
3537
/**
3638
* @function
@@ -46,6 +48,11 @@ export const useGeoLocationStore = defineStore('plugins/geoLocation', () => {
4648
const lastBoundaryCheck = ref<boolean | symbol | null>(null)
4749
const position = ref<number[]>([])
4850

51+
let onMapPointerDrag: (() => void) | null = null
52+
let onPointerDown: ((e: PointerEvent) => void) | null = null
53+
let onPointerUp: ((e: PointerEvent) => void) | null = null
54+
const activePointers = new Map<number, number>()
55+
4956
const configuration = computed<
5057
GeoLocationPluginOptions & { showTooltip: boolean; zoomLevel: number }
5158
>(() =>
@@ -144,6 +151,7 @@ export const useGeoLocationStore = defineStore('plugins/geoLocation', () => {
144151
})
145152
return
146153
}
154+
isUserInteracting = false
147155
if (geolocation.value === null) {
148156
geolocation.value = new Geolocation({
149157
trackingOptions: {
@@ -156,6 +164,29 @@ export const useGeoLocationStore = defineStore('plugins/geoLocation', () => {
156164
} else {
157165
void positioning()
158166
}
167+
168+
// Track pointer count to distinguish 1-finger drag from multi-touch
169+
const viewport = coreStore.map.getViewport()
170+
171+
onPointerDown = (e: PointerEvent) => {
172+
activePointers.set(e.pointerId, 1)
173+
}
174+
175+
onPointerUp = (e: PointerEvent) => {
176+
activePointers.delete(e.pointerId)
177+
}
178+
179+
viewport.addEventListener('pointerdown', onPointerDown)
180+
viewport.addEventListener('pointerup', onPointerUp)
181+
182+
// Handler for pointerdrag - only set isUserInteracting if exactly 1 pointer
183+
onMapPointerDrag = () => {
184+
if (activePointers.size === 1) {
185+
isUserInteracting = true
186+
}
187+
}
188+
189+
coreStore.map.on('pointerdrag', onMapPointerDrag)
159190
geolocation.value.on('change:position', positioning)
160191
geolocation.value.on('change:heading', ({ target }) => {
161192
markerFeature.set('heading', target.getHeading())
@@ -185,6 +216,22 @@ export const useGeoLocationStore = defineStore('plugins/geoLocation', () => {
185216
function untrack() {
186217
// For FireFox - cannot handle geolocation.un(...).
187218
geolocation.value?.setTracking(false)
219+
220+
if (onMapPointerDrag) {
221+
coreStore.map.un('pointerdrag', onMapPointerDrag)
222+
onMapPointerDrag = null
223+
}
224+
const viewport = coreStore.map.getViewport()
225+
if (onPointerDown) {
226+
viewport.removeEventListener('pointerdown', onPointerDown)
227+
onPointerDown = null
228+
}
229+
if (onPointerUp) {
230+
viewport.removeEventListener('pointerup', onPointerUp)
231+
onPointerUp = null
232+
}
233+
activePointers.clear()
234+
188235
removeMarker()
189236
geolocation.value = null
190237
}
@@ -242,7 +289,8 @@ export const useGeoLocationStore = defineStore('plugins/geoLocation', () => {
242289

243290
if (
244291
(configuration.value.keepCentered || !hadPosition) &&
245-
lastBoundaryCheck.value
292+
lastBoundaryCheck.value &&
293+
!isUserInteracting
246294
) {
247295
coreStore.map.getView().setCenter(coordinate)
248296
coreStore.map.getView().setZoom(configuration.value.zoomLevel)

0 commit comments

Comments
 (0)