Skip to content

Commit 6036015

Browse files
Merge pull request #566 from Dataport/vue3/update-behaviour-of-GeoLocation
refactor(geoLocation): update centering behaviour
2 parents 67148fb + 26ab1e3 commit 6036015

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

examples/snowbox/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ addPlugin(
359359
{
360360
plugin: pluginGeoLocation({
361361
checkLocationInitially: false,
362-
keepCentered: false,
362+
keepCentered: true,
363363
showTooltip: true,
364364
zoomLevel: 7,
365365
// usable when you're in HH or fake your geolocation to HH

src/core/stores/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ export const useCoreStore = defineStore('core', () => {
2929
const markerStore = useMarkerStore()
3030

3131
return {
32+
/**
33+
* The current center coordinates of the map.
34+
*
35+
* @alpha
36+
* @readonly
37+
*/
38+
center: computed(() => mainStore.center),
39+
3240
/**
3341
* Color scheme the client should be using.
3442
*/

src/plugins/geoLocation/store.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import * as Proj from 'ol/proj'
1818
import { transform as transformCoordinates } from 'ol/proj'
1919
import VectorSource from 'ol/source/Vector'
2020
import { defineStore } from 'pinia'
21-
import { computed, ref } from 'vue'
21+
import { computed, ref, watch } from 'vue'
2222

2323
import { useCoreStore } from '@/core/stores'
2424
import { notifyUser } from '@/lib/notifyUser'
@@ -46,6 +46,8 @@ export const useGeoLocationStore = defineStore('plugins/geoLocation', () => {
4646
const lastBoundaryCheck = ref<boolean | symbol | null>(null)
4747
const position = ref<number[]>([])
4848

49+
let mapHasBeenMovedByUser = false
50+
4951
const configuration = computed<
5052
GeoLocationPluginOptions & { showTooltip: boolean; zoomLevel: number }
5153
>(() =>
@@ -141,6 +143,7 @@ export const useGeoLocationStore = defineStore('plugins/geoLocation', () => {
141143

142144
/** Enable tracking of geo position */
143145
function track() {
146+
mapHasBeenMovedByUser = false
144147
if (isGeolocationDenied.value) {
145148
onError({
146149
message: 'Geolocation API usage was denied by user or configuration.',
@@ -166,6 +169,15 @@ export const useGeoLocationStore = defineStore('plugins/geoLocation', () => {
166169
geolocation.value.on('error', onError)
167170
}
168171

172+
watch(
173+
() => coreStore.center,
174+
() => {
175+
if (position.value !== coreStore.center) {
176+
mapHasBeenMovedByUser = true
177+
}
178+
}
179+
)
180+
169181
/**
170182
* Show error information and stop tracking if there are errors by tracking the position
171183
*/
@@ -245,7 +257,8 @@ export const useGeoLocationStore = defineStore('plugins/geoLocation', () => {
245257

246258
if (
247259
(configuration.value.keepCentered || !hadPosition) &&
248-
lastBoundaryCheck.value
260+
lastBoundaryCheck.value &&
261+
!mapHasBeenMovedByUser
249262
) {
250263
coreStore.map.getView().setCenter(coordinate)
251264
coreStore.map.getView().setZoom(configuration.value.zoomLevel)

0 commit comments

Comments
 (0)