Files
Wegwichtel/public/js/geolocation.js
T
Florian Zumpe 171ea88d55
Sonarqube Scanner / Prepare environment (push) Successful in 8s
Sonarqube Scanner / Build and test (push) Failing after 22s
Sonarqube Scanner / SonarQube analysis (push) Skipped
Sonarqube Scanner / Deploy to Server (push) Successful in 6s
overhaul gpx creation and usage
2026-10-03 12:40:26 +02:00

75 lines
1.6 KiB
JavaScript

(function (ns) {
'use strict';
const WATCH_OPTIONS = Object.freeze({
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 5000
});
let watchId = null;
function geolocation() {
return globalThis.navigator?.geolocation;
}
function normalize(position) {
return {
lat: position.coords.latitude,
lon: position.coords.longitude,
accuracy: position.coords.accuracy,
altitude: position.coords.altitude,
heading: position.coords.heading,
speed: position.coords.speed,
timestamp: position.timestamp
};
}
function unsupportedError() {
return new Error(ns.I18n.t("location.error.unsupported"));
}
ns.Geo = {
current(options = {}) {
return new Promise((resolve, reject) => {
const service = geolocation();
if (!service) {
reject(unsupportedError());
return;
}
service.getCurrentPosition(
position => resolve(normalize(position)),
reject,
{
...WATCH_OPTIONS,
timeout: ns.Config.geolocationTimeoutMs,
maximumAge: 30000,
...options
}
);
});
},
start(callback, onError) {
this.stop();
const service = geolocation();
if (!service) {
onError?.(unsupportedError());
return;
}
watchId = service.watchPosition(
position => callback(normalize(position)),
onError,
WATCH_OPTIONS
);
},
stop() {
const service = geolocation();
if (watchId != null) service?.clearWatch(watchId);
watchId = null;
}
};
}(globalThis.Wegwichtel));