Skip to content

Commit 0bcbe9a

Browse files
Merge pull request #166 from Dataport/feature/geolocation-outside-extent-or-boundary
Feature/geolocation outside extent or boundary
2 parents f070459 + 56c8be9 commit 0bcbe9a

File tree

7 files changed

+46
-30
lines changed

7 files changed

+46
-30
lines changed

packages/plugins/GeoLocation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## unpublished
44

5+
- Feature: Position is now tracked when user is outside of the boundary layer but inside the map extent.
56
- Fix: Adjust documentation to properly describe optionality of configuration parameters.
67
- Fix: Add missing peerDependency `ol@^9.2.4`.
78

packages/plugins/GeoLocation/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ either `true` or `false`. When a users denies the location tracking, the button
1212

1313
| fieldName | type | description |
1414
| - | - | - |
15-
| boundaryLayerId | string? | Id of a vector layer to restrict geolocation markers and zooms to. When geolocation outside of its features occurs, an information will be shown once and the feature is stopped. The map will wait at most 10s for the layer to load; should it not happen, the geolocation feature is turned off. |
15+
| boundaryLayerId | string? | Id of a vector layer to restrict geolocation markers and zooms to. When geolocation outside of its features occurs, a single information will be shown. When loading the boundary layer, the map will wait at most 10s; should it not happen, the geolocation feature is turned off. |
1616
| boundaryOnError | ('strict' \| 'permissive')? | If the boundary layer check does not work due to loading or configuration errors, style `'strict'` will disable the geolocation feature, and style `'permissive'` will act as if no boundaryLayerId was set. Defaults to `'permissive'`. |
1717
| checkLocationInitially | boolean? | If `true` the location gets checked on page load. When `false` this can be triggered with a button. Defaults to `false`. |
1818
| keepCentered | boolean? | If `true`, the map will re-center on the user on any position change. If `false`, only the first position will be centered on. Defaults to `false`. |

packages/plugins/GeoLocation/src/store/actions.ts

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
/* eslint-disable max-lines-per-function */
12
import { PolarActionTree } from '@polar/lib-custom-types'
2-
import { passesBoundaryCheck } from '@polar/lib-passes-boundary-check'
33
import VectorLayer from 'ol/layer/Vector'
44
import Point from 'ol/geom/Point'
55
import { Vector } from 'ol/source'
@@ -11,8 +11,12 @@ import Geolocation from 'ol/Geolocation.js'
1111
import { transform as transformCoordinates } from 'ol/proj'
1212
import Overlay from 'ol/Overlay'
1313
import { getTooltip } from '@polar/lib-tooltip'
14+
import { passesBoundaryCheck } from '@polar/lib-passes-boundary-check'
1415
import { GeoLocationState, GeoLocationGetters } from '../types'
1516
import geoLocationMarker from '../assets/geoLocationMarker'
17+
import positionChanged from '../utils/positionChanged'
18+
19+
let boundaryCheckChanged = true
1620

1721
const actions: PolarActionTree<GeoLocationState, GeoLocationGetters> = {
1822
setupModule({ getters, commit, dispatch }): void {
@@ -118,11 +122,12 @@ const actions: PolarActionTree<GeoLocationState, GeoLocationGetters> = {
118122
async positioning({
119123
rootGetters: { map, configuration },
120124
getters: {
121-
boundaryLayerId,
122125
boundaryOnError,
126+
boundaryLayerId,
123127
geolocation,
124128
configuredEpsg,
125129
position,
130+
boundaryCheck,
126131
},
127132
commit,
128133
dispatch,
@@ -132,38 +137,36 @@ const actions: PolarActionTree<GeoLocationState, GeoLocationGetters> = {
132137
Proj.get('EPSG:4326') as Proj.Projection,
133138
configuredEpsg
134139
)
135-
136-
const boundaryCheckPassed =
137-
typeof boundaryLayerId === 'string'
138-
? await passesBoundaryCheck(map, boundaryLayerId, transformedCoords)
139-
: containsCoordinate(
140-
// NOTE: The fallback is the default value set by @masterportal/masterportalapi
141-
configuration?.extent || [510000.0, 5850000.0, 625000.4, 6000000.0],
142-
transformedCoords
143-
)
144-
const boundaryErrorOccurred = typeof boundaryCheckPassed === 'symbol'
145-
146-
if (
147-
boundaryCheckPassed === false ||
148-
(boundaryErrorOccurred && boundaryOnError !== 'permissive')
149-
) {
150-
dispatch('printPositioningFailed', boundaryErrorOccurred)
151-
// if check initially breaks or user leaves boundary, turn off tracking
140+
const coordinateInExtent = containsCoordinate(
141+
// NOTE: The fallback is the default value set by @masterportal/masterportalApi
142+
configuration?.extent || [510000.0, 5850000.0, 625000.4, 6000000.0],
143+
transformedCoords
144+
)
145+
const boundaryCheckPassed = await passesBoundaryCheck(
146+
map,
147+
boundaryLayerId,
148+
transformedCoords
149+
)
150+
boundaryCheckChanged = boundaryCheck !== boundaryCheckPassed
151+
commit('setBoundaryCheck', boundaryCheckPassed)
152+
const showBoundaryLayerError =
153+
typeof boundaryCheckPassed === 'symbol' && boundaryOnError === 'strict'
154+
if (!coordinateInExtent || showBoundaryLayerError) {
155+
dispatch('printPositioningFailed', showBoundaryLayerError)
152156
dispatch('untrack')
153157
return
154158
}
155-
156-
if (
157-
position[0] !== transformedCoords[0] ||
158-
position[1] !== transformedCoords[1]
159-
) {
159+
if (positionChanged(position, transformedCoords)) {
160160
commit('setPosition', transformedCoords)
161161
dispatch('addMarker', transformedCoords)
162+
if (boundaryCheckChanged && !boundaryCheckPassed) {
163+
dispatch('printPositioningFailed', false)
164+
}
162165
}
163166
},
164167
printPositioningFailed(
165168
{ dispatch, getters: { toastAction } },
166-
boundaryErrorOccurred: string
169+
boundaryErrorOccurred: boolean
167170
) {
168171
if (toastAction) {
169172
const toast = boundaryErrorOccurred
@@ -194,7 +197,12 @@ const actions: PolarActionTree<GeoLocationState, GeoLocationGetters> = {
194197
*/
195198
addMarker(
196199
{
197-
getters: { geoLocationMarkerLayer, markerFeature, keepCentered },
200+
getters: {
201+
geoLocationMarkerLayer,
202+
markerFeature,
203+
keepCentered,
204+
boundaryCheck,
205+
},
198206
dispatch,
199207
},
200208
coordinates
@@ -213,8 +221,7 @@ const actions: PolarActionTree<GeoLocationState, GeoLocationGetters> = {
213221
}),
214222
})
215223
)
216-
217-
if (keepCentered || !hadPosition) {
224+
if ((keepCentered || !hadPosition) && boundaryCheck) {
218225
dispatch('zoomAndCenter')
219226
}
220227
},

packages/plugins/GeoLocation/src/store/getInitialState.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const getInitialState = (): GeoLocationState => ({
55
position: [],
66
tracking: false,
77
isGeolocationDenied: false,
8+
boundaryCheck: null,
89
})
910

1011
export default getInitialState

packages/plugins/GeoLocation/src/store/getters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const getters: PolarGetterTree<GeoLocationState, GeoLocationGetters> = {
3838
return rootGetters.configuration?.geoLocation?.toastAction
3939
},
4040
zoomLevel: (_, __, ___, rootGetters): number => {
41-
return rootGetters.configuration?.geoLocation?.zoomLevel || 7
41+
return rootGetters.configuration?.geoLocation?.zoomLevel ?? 7
4242
},
4343
geoLocationMarkerLayer(_, __, ___, rootGetters) {
4444
return rootGetters?.map

packages/plugins/GeoLocation/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface GeoLocationState {
88
position: number[]
99
tracking: boolean
1010
isGeolocationDenied: boolean
11+
boundaryCheck: boolean | symbol | null
1112
}
1213

1314
export interface GeoLocationGetters extends GeoLocationState {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default function positionChanged(
2+
oldPosition: number[],
3+
newPosition: number[]
4+
): boolean {
5+
return oldPosition[0] !== newPosition[0] || oldPosition[1] !== newPosition[1]
6+
}

0 commit comments

Comments
 (0)