Wegwichtel/public/js/geolocation.js
2026-06-16 10:31:41 +02:00

19 lines
952 B
JavaScript

(function (ns) {
'use strict';
let watchId = null;
function normalize(position) { return { lat: position.coords.latitude, lon: position.coords.longitude, accuracy: position.coords.accuracy, timestamp: position.timestamp }; }
ns.Geo = {
current() {
return new Promise((resolve, reject) => {
if (!navigator.geolocation) return reject(new Error('Dieses Gerät unterstützt keine Geolokalisierung.'));
navigator.geolocation.getCurrentPosition(p => resolve(normalize(p)), reject, { enableHighAccuracy: true, timeout: ns.Config.geolocationTimeoutMs, maximumAge: 30000 });
});
},
start(callback, onError) {
this.stop();
watchId = navigator.geolocation.watchPosition(p => callback(normalize(p)), onError, { enableHighAccuracy: true, timeout: 20000, maximumAge: 5000 });
},
stop() { if (watchId != null) navigator.geolocation.clearWatch(watchId); watchId = null; }
};
}(window.Wegwichtel));