Skip to content

Commit 3527d36

Browse files
committed
Use navigator.watchPosition instead of a timed loop
1 parent 806b58a commit 3527d36

4 files changed

Lines changed: 33 additions & 12 deletions

File tree

assets/view_map.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ require('leaflet');
22

33
if (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) {

templates/view_post.html.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
data-measurementtype="{{ setting('locationMeasurement') }}"
9696
data-measurement="{{ (setting('locationMeasurement') == 'km') ? 'measurement_units.kilometers.label.short' | trans : 'measurement_units.miles.label' | trans }}"
9797
data-away="{{ 'measurement_units.away' | trans }}"
98+
data-error="{{ 'map.validations.location_disabled' | trans }}"
9899
></div>
99100
</div>
100101
</div>

translations/messages.en.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ map:
322322
invalid_location: "You must choose a valid location"
323323
invalid_location_size: "You must choose a valid location size"
324324
out_of_bounds: "The location can only be set inside the bounds of this Habitat"
325+
location_disabled: "You need to enable access to your location to do that"
325326

326327
user_settings:
327328
title: "Settings"

translations/messages.it.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ map:
322322
invalid_location: "Devi scegliere una posizione valida"
323323
invalid_location_size: "Devi scegliere una dimensione della posizione valida"
324324
out_of_bounds: "La posizione può essere impostata solo all'interno dei confini di questo Habitat"
325+
location_disabled: "Devi abilitare l'accesso alla tua posizione per farlo"
325326

326327
user_settings:
327328
title: "Impostazioni"

0 commit comments

Comments
 (0)