Files
Wegwichtel/public/js/geolocation.js
T
Florian Zumpe 5c4d20bd1d
Sonarqube Scanner / Prepare environment (push) Successful in 19s
Sonarqube Scanner / Build and test (push) Successful in 43s
Sonarqube Scanner / SonarQube analysis (push) Failing after 2m28s
Sonarqube Scanner / Deploy to Server (push) Successful in 16s
fix sonar findings and translation structure
2026-10-01 16:31:46 +02:00

74 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() {
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
}
);
});
},
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));