11import { Feature , Map } from 'ol'
2- import { useEffect , useRef } from 'react'
2+ import { useEffect } from 'react'
33import VectorLayer from 'ol/layer/Vector'
44import VectorSource from 'ol/source/Vector'
5- import { Circle as CircleGeom , Point } from 'ol/geom'
5+ import { Circle , Circle as CircleGeom , Point } from 'ol/geom'
66import { Circle as CircleStyle , Fill , Stroke , Style } from 'ol/style'
77import { CurrentLocationStoreState } from '@/stores/CurrentLocationStore'
88import { fromLonLat } from 'ol/proj'
9- import { getMap } from '@/map/map'
10- import Dispatcher from '@/stores/Dispatcher'
119
1210const LOCATION_LAYER_KEY = 'gh:current_location'
1311
1412export 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}
0 commit comments