Skip to content

Commit d11148d

Browse files
committed
fix: update zoom and location in 1 anim
1 parent 83a5d82 commit d11148d

3 files changed

Lines changed: 7 additions & 29 deletions

File tree

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
<div id="zoomMinusButton" onclick="zoomOut()">
9999
<i class="bi bi-dash"></i>
100100
</div>
101-
<div id="getLocationButton" onclick="getLocation()">
101+
<div id="getLocationButton" onclick="getLocation(true, true)">
102102
<i class="bi bi-crosshair"></i>
103103
</div>
104104
</div>

scripts/map.js

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ function zoomOut() {
303303
});
304304
}
305305

306-
function getLocation(zoom = true) {
306+
function getLocation(zoom = true, followLocationOverride) {
307+
if (typeof followLocationOverride === "boolean") followLocation = followLocationOverride;
307308
// Create the current position icon feature
308309
const iconFeature = new ol.Feature({
309310
geometry: new ol.geom.Point(ol.proj.fromLonLat(pos ?? [0, 0])),
@@ -416,14 +417,14 @@ function getLocation(zoom = true) {
416417
const view = map.getView();
417418
const distanceBetweenUpdates = distance(ol.proj.toLonLat(view.getCenter()), pos);
418419
document.getElementById("distanceBetweenUpdates").innerHTML = distanceBetweenUpdates.toFixed(2) + "m";
420+
const currentZoom = view.getZoom();
419421
if (distanceBetweenUpdates > 5) {
420422
view.animate({
421423
center: ol.proj.fromLonLat(pos),
422-
zoom: map.getView().getZoom(), // use the current zoom
424+
zoom: zoom && currentZoom < 13.5 ? 16 : currentZoom, // use the current zoom
423425
duration: 500,
424426
});
425-
}
426-
//map.getView().setCenter(ol.proj.fromLonLat(pos));
427+
} else if (zoom) view.setZoom(currentZoom < 13.5 ? 16 : currentZoom);
427428
}
428429

429430
if (navigationActive && rotationMode === "route") updateRotation();
@@ -454,29 +455,6 @@ function getLocation(zoom = true) {
454455
}
455456
);
456457
watchPositionIDs.push(watchPositionID);
457-
458-
// Pan to location only once when position has been set
459-
if (zoom) {
460-
const locationPromise = new Promise((resolve, reject) => {
461-
const loop = () => (pos ? resolve(pos) : setTimeout(loop));
462-
loop();
463-
});
464-
465-
locationPromise.then(pos => {
466-
// Pan to location
467-
const currentZoom = map.getView().getZoom();
468-
469-
const view = map.getView();
470-
view.animate({
471-
center: ol.proj.fromLonLat(pos),
472-
zoom: currentZoom < 13.5 ? 16 : currentZoom,
473-
duration: 100,
474-
});
475-
476-
// Only set the follow location at the end, so that the one time zoom works
477-
if (!followLocation) followLocation = true;
478-
});
479-
}
480458
} else {
481459
// Browser doesn't support Geolocation
482460
console.log("Error: Your browser doesn't support geolocation.");

scripts/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ async function runStartupFunctions() {
125125
showUpdateInfoIfNeeded();
126126

127127
// Get the user location on app open
128-
getLocation();
128+
getLocation(true, true);
129129

130130
// Start rotation of location dot
131131
// startLocationDotRotation();

0 commit comments

Comments
 (0)