Skip to content

Commit 3be532f

Browse files
committed
show accurracy radius and show search animation
1 parent f224255 commit 3be532f

File tree

4 files changed

+40
-39
lines changed

4 files changed

+40
-39
lines changed

src/actions/Actions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,10 @@ export class CurrentLocationError implements Action {
295295

296296
export class CurrentLocation implements Action {
297297
readonly coordinate: Coordinate
298+
readonly accuracy: number
298299

299-
constructor(coordinate: Coordinate) {
300+
constructor(coordinate: Coordinate, accuracy: number) {
300301
this.coordinate = coordinate
302+
this.accuracy = accuracy
301303
}
302304
}

src/layers/UseCurrentLocationLayer.tsx

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,39 @@
11
import { Feature, Map } from 'ol'
2-
import { useEffect, useRef } from 'react'
2+
import { useEffect } from 'react'
33
import VectorLayer from 'ol/layer/Vector'
44
import VectorSource from 'ol/source/Vector'
5-
import { Circle as CircleGeom, Point } from 'ol/geom'
5+
import { Circle, Circle as CircleGeom, Point } from 'ol/geom'
66
import { Circle as CircleStyle, Fill, Stroke, Style } from 'ol/style'
77
import { CurrentLocationStoreState } from '@/stores/CurrentLocationStore'
88
import { fromLonLat } from 'ol/proj'
9-
import { getMap } from '@/map/map'
10-
import Dispatcher from '@/stores/Dispatcher'
119

1210
const LOCATION_LAYER_KEY = 'gh:current_location'
1311

1412
export default function useCurrentLocationLayer(map: Map, locationState: CurrentLocationStoreState) {
1513
useEffect(() => {
16-
console.log('NOW useEffect, syncView = ', locationState.syncView)
17-
1814
if (!locationState.enabled) {
1915
removeCurrentLocationLayer(map)
2016
return
2117
}
2218

2319
const positionFeature = new Feature()
20+
const accuracyFeature = new Feature()
2421
if (locationState.coordinate) {
25-
positionFeature.setGeometry(
26-
new Point(fromLonLat([locationState.coordinate.lng, locationState.coordinate.lat]))
27-
)
22+
const coord = fromLonLat([locationState.coordinate.lng, locationState.coordinate.lat])
23+
positionFeature.setGeometry(new Point(coord))
24+
accuracyFeature.setGeometry(new Circle(coord, locationState.accuracy))
2825

2926
if (locationState.syncView) {
3027
// TODO same code as for MoveMapToPoint action, but calling Dispatcher here is ugly
3128
let zoom = map.getView().getZoom()
3229
if (zoom == undefined || zoom < 8) zoom = 8
33-
map.getView().animate({
34-
zoom: zoom,
35-
center: fromLonLat([locationState.coordinate.lng, locationState.coordinate.lat]),
36-
duration: 400,
37-
})
30+
map.getView().animate({ zoom: zoom, center: coord, duration: 400 })
3831
}
3932
}
4033

41-
const layer = createLocationLayer(positionFeature)
34+
const layer = createLocationLayer()
35+
layer.getSource()?.addFeature(positionFeature)
36+
layer.getSource()?.addFeature(accuracyFeature)
4237
map.addLayer(layer)
4338

4439
return () => {
@@ -54,7 +49,7 @@ function removeCurrentLocationLayer(map: Map) {
5449
.forEach(l => map.removeLayer(l))
5550
}
5651

57-
function createLocationLayer(positionFeature: Feature): VectorLayer<VectorSource> {
52+
function createLocationLayer(): VectorLayer<VectorSource> {
5853
const layer = new VectorLayer({
5954
source: new VectorSource(),
6055
style: feature => {
@@ -86,22 +81,20 @@ function createLocationLayer(positionFeature: Feature): VectorLayer<VectorSource
8681
]
8782
} else if (geometry instanceof CircleGeom) {
8883
// Accuracy circle style
89-
// return new Style({
90-
// fill: new Fill({
91-
// color: 'rgba(66, 133, 244, 0.1)'
92-
// }),
93-
// stroke: new Stroke({
94-
// color: 'rgba(66, 133, 244, 0.3)',
95-
// width: 1
96-
// })
97-
// })
84+
return new Style({
85+
fill: new Fill({
86+
color: 'rgba(66, 133, 244, 0.1)',
87+
}),
88+
stroke: new Stroke({
89+
color: 'rgba(66, 133, 244, 0.3)',
90+
width: 1,
91+
}),
92+
})
9893
}
9994
return []
10095
},
10196
})
10297

10398
layer.setZIndex(4) // Above paths and query points
104-
105-
layer.getSource()?.addFeature(positionFeature)
10699
return layer
107100
}

src/map/LocationButton.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ export default function LocationButton(props: { currentLocation: CurrentLocation
1515
if (props.currentLocation.enabled) {
1616
if (!props.currentLocation.syncView) setLocationSearch('on_but_not_in_sync')
1717
else if (props.currentLocation.error) setLocationSearch('error')
18-
else setLocationSearch('initial')
18+
else if (props.currentLocation.coordinate != null) setLocationSearch('initial')
19+
else setLocationSearch('search')
1920
}
20-
}, [props.currentLocation.syncView, props.currentLocation.error, props.currentLocation.enabled])
21+
}, [
22+
props.currentLocation.syncView,
23+
props.currentLocation.error,
24+
props.currentLocation.enabled,
25+
props.currentLocation.coordinate,
26+
])
2127

2228
return (
2329
<div

src/stores/CurrentLocationStore.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import Dispatcher, { Action } from '@/stores/Dispatcher'
33
import {
44
CurrentLocation,
55
CurrentLocationError,
6-
MoveMapToPoint,
76
StartSyncCurrentLocation,
87
StartWatchCurrentLocation,
98
StopSyncCurrentLocation,
@@ -16,6 +15,7 @@ export interface CurrentLocationStoreState {
1615
error: string | null
1716
enabled: boolean
1817
syncView: boolean
18+
accuracy: number // meters
1919
coordinate: Coordinate | null
2020
}
2121

@@ -27,6 +27,7 @@ export default class CurrentLocationStore extends Store<CurrentLocationStoreStat
2727
error: null,
2828
enabled: false,
2929
syncView: false,
30+
accuracy: 0,
3031
coordinate: null,
3132
})
3233
}
@@ -38,7 +39,6 @@ export default class CurrentLocationStore extends Store<CurrentLocationStoreStat
3839
return state
3940
}
4041

41-
console.log('NOW start ' + JSON.stringify(action, null, 2))
4242
this.start()
4343
return {
4444
...state,
@@ -48,7 +48,7 @@ export default class CurrentLocationStore extends Store<CurrentLocationStoreStat
4848
coordinate: null,
4949
}
5050
} else if (action instanceof StopWatchCurrentLocation) {
51-
console.log('NOW stop ' + JSON.stringify(action, null, 2))
51+
// TODO NOW stop location watching e.g. when pressing the location button once again if it shows 'success'
5252
this.stop()
5353
return {
5454
...state,
@@ -57,26 +57,24 @@ export default class CurrentLocationStore extends Store<CurrentLocationStoreStat
5757
syncView: false,
5858
}
5959
} else if (action instanceof CurrentLocationError) {
60-
console.log('NOW error ' + JSON.stringify(action, null, 2))
6160
return {
6261
...state,
6362
enabled: false,
6463
error: action.error,
6564
coordinate: null,
6665
}
6766
} else if (action instanceof CurrentLocation) {
68-
console.log('NOW current ' + JSON.stringify(action, null, 2))
6967
return {
7068
...state,
69+
accuracy: action.accuracy,
7170
coordinate: action.coordinate,
7271
}
7372
} else if (action instanceof StartSyncCurrentLocation) {
7473
if (!state.enabled) {
75-
console.log('NOW cannot start sync as not enabled')
74+
console.warn('cannot start synchronizing view as current location not enabled')
7675
return state
7776
}
7877

79-
console.log('NOW start sync ' + JSON.stringify(action, null, 2))
8078
return {
8179
...state,
8280
error: null,
@@ -86,7 +84,6 @@ export default class CurrentLocationStore extends Store<CurrentLocationStoreStat
8684
} else if (action instanceof StopSyncCurrentLocation) {
8785
if (!state.enabled) return state
8886

89-
console.log('NOW stop sync ' + JSON.stringify(action, null, 2))
9087
return {
9188
...state,
9289
error: null,
@@ -106,7 +103,10 @@ export default class CurrentLocationStore extends Store<CurrentLocationStoreStat
106103
this.watchId = navigator.geolocation.watchPosition(
107104
position => {
108105
Dispatcher.dispatch(
109-
new CurrentLocation({ lng: position.coords.longitude, lat: position.coords.latitude })
106+
new CurrentLocation(
107+
{ lng: position.coords.longitude, lat: position.coords.latitude },
108+
position.coords.accuracy
109+
)
110110
)
111111
},
112112
error => {

0 commit comments

Comments
 (0)