62 lines
1.5 KiB
JavaScript
62 lines
1.5 KiB
JavaScript
(function (ns) {
|
|
'use strict';
|
|
|
|
let watchId = null;
|
|
|
|
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
|
|
};
|
|
}
|
|
|
|
ns.Geo = {
|
|
current() {
|
|
return new Promise((resolve, reject) => {
|
|
if (!navigator.geolocation) {
|
|
reject(new Error('Dieses Gerät unterstützt keine Geolokalisierung.'));
|
|
return;
|
|
}
|
|
|
|
navigator.geolocation.getCurrentPosition(
|
|
position => resolve(normalize(position)),
|
|
reject,
|
|
{
|
|
enableHighAccuracy: true,
|
|
timeout: ns.Config.geolocationTimeoutMs,
|
|
maximumAge: 30000
|
|
}
|
|
);
|
|
});
|
|
},
|
|
|
|
start(callback, onError) {
|
|
this.stop();
|
|
if (!navigator.geolocation) {
|
|
onError?.(new Error('Dieses Gerät unterstützt keine Geolokalisierung.'));
|
|
return;
|
|
}
|
|
|
|
watchId = navigator.geolocation.watchPosition(
|
|
position => callback(normalize(position)),
|
|
onError,
|
|
{
|
|
enableHighAccuracy: true,
|
|
timeout: 20000,
|
|
maximumAge: 5000
|
|
}
|
|
);
|
|
},
|
|
|
|
stop() {
|
|
if (watchId != null) navigator.geolocation.clearWatch(watchId);
|
|
watchId = null;
|
|
}
|
|
};
|
|
}(window.Wegwichtel));
|