@@ -2,6 +2,7 @@ require('leaflet');
22
33if ( document . getElementById ( 'map' ) !== null ) {
44 var location ;
5+ var watchPosition ;
56 var myLocationEnabled = false ;
67 var myLocationRefresh = false ;
78 var relativeDistanceControl ;
@@ -33,6 +34,7 @@ if (document.getElementById('map') !== null) {
3334 viewLocationButton . id = 'view-location-btn' ;
3435 viewLocationButton . innerHTML = '<i class="bi bi-compass"></i>' ;
3536 viewLocationButton . title = document . getElementById ( 'map' ) . dataset . showcurrentlocation ;
37+ viewLocationButton . setAttribute ( 'data-test' , 'current-location-btn' )
3638
3739 return viewLocationButton ;
3840 } ,
@@ -62,15 +64,10 @@ if (document.getElementById('map') !== null) {
6264 if ( myLocationEnabled ) {
6365 viewLocationButton . title = document . getElementById ( 'map' ) . dataset . hidecurrentlocation ;
6466 document . getElementById ( 'view-location-btn' ) . classList . add ( 'leaflet-btn-active' ) ;
65- navigator . geolocation . getCurrentPosition ( setCurrentPosition ) ;
67+ watchPosition = navigator . geolocation . watchPosition ( setCurrentPosition , failCurrentPosition ) ;
6668 } else {
67- viewLocationButton . title = document . getElementById ( 'map' ) . dataset . showcurrentlocation ;
68- document . getElementById ( 'view-location-btn' ) . classList . remove ( 'leaflet-btn-active' ) ;
69-
70- relativeDistanceControl . remove ( ) ;
71- relativeDistanceControl = undefined ;
72- myLocationRefresh = false ;
73- centerOnMarker ( ) ;
69+ navigator . geolocation . clearWatch ( watchPosition ) ;
70+ disableCurrentLocation ( ) ;
7471 }
7572 }
7673
@@ -87,6 +84,30 @@ if (document.getElementById('map') !== null) {
8784 map . panTo ( latLng ) ;
8885 }
8986
87+ function failCurrentPosition ( err ) {
88+ switch ( err . code ) {
89+ case 1 :
90+ alert ( document . getElementById ( 'map' ) . dataset . error ) ;
91+ break ;
92+ default :
93+ alert ( err . message ) ;
94+ }
95+
96+ disableCurrentLocation ( ) ;
97+ }
98+
99+ function disableCurrentLocation ( ) {
100+ viewLocationButton . title = document . getElementById ( 'map' ) . dataset . showcurrentlocation ;
101+ document . getElementById ( 'view-location-btn' ) . classList . remove ( 'leaflet-btn-active' ) ;
102+
103+ if ( relativeDistanceControl !== undefined ) {
104+ relativeDistanceControl . remove ( ) ;
105+ relativeDistanceControl = undefined ;
106+ }
107+ myLocationRefresh = false ;
108+ centerOnMarker ( ) ;
109+ }
110+
90111 function setCurrentPosition ( position ) {
91112 if ( ! myLocationEnabled ) {
92113 return ;
@@ -127,6 +148,7 @@ if (document.getElementById('map') !== null) {
127148 distanceIndicator . id = 'distanceIndicator' ;
128149 distanceIndicator . title = document . getElementById ( 'map' ) . dataset . showcurrentlocation ;
129150 distanceIndicator . innerHTML = printDistance ( map . distance ( currentLatLng , latLng ) ) ;
151+ distanceIndicator . setAttribute ( 'data-test' , 'distance-indicator' )
130152
131153 return distanceIndicator ;
132154 } ,
@@ -145,10 +167,6 @@ if (document.getElementById('map') !== null) {
145167
146168 line = L . polyline ( [ latLng , currentLatLng ] , { color : 'white' , weight : 6 , dashArray : '10,14' , className : 'location-path' } ) ;
147169 line . addTo ( map ) ;
148-
149- setTimeout ( function ( ) {
150- navigator . geolocation . getCurrentPosition ( setCurrentPosition ) ;
151- } , 5000 ) ;
152170 }
153171
154172 function printDistance ( meters ) {
0 commit comments