Move geolocation service

This commit is contained in:
Alessio Davoli 2023-07-18 17:47:35 +02:00
parent a3e6809e24
commit 6a28949f41
2 changed files with 42 additions and 36 deletions

View file

@ -8,6 +8,42 @@
let map;
let layerGroup;
const loadWorker = async () => {
if (!("geolocation" in navigator)) {
/* geolocation is not available */
return;
}
const options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0,
};
function success(pos) {
const crd = pos.coords;
let string = "";
latitude = crd.latitude;
longitude = crd.longitude;
accuracy = crd.accuracy;
string += "Your current position is:";
string += `Latitude : ${crd.latitude}`;
string += `Longitude: ${crd.longitude}`;
string += `More or less ${crd.accuracy} meters.`;
}
function error(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
}
navigator.geolocation.watchPosition(success, error, options);
};
onMount(async () => {
leaflet = await import('leaflet');
layerGroup = leaflet.layerGroup();
@ -28,6 +64,8 @@
'&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
})
.addTo(map);
loadWorker();
});
onDestroy(async () => {

View file

@ -6,42 +6,10 @@
let syncWorker: Worker | undefined = undefined;
const loadWorker = async () => {
if (!("geolocation" in navigator)) {
/* geolocation is not available */
return;
}
// const watchID = navigator.geolocation.watchPosition((position) => {
// latitude = position.coords.latitude;
// longitude = position.coords.longitude;
// console.log({latitude, longitude});
// });
const options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0,
};
function success(pos) {
const crd = pos.coords;
let string = "";
string += "Your current position is:";
string += `Latitude : ${crd.latitude}`;
string += `Longitude: ${crd.longitude}`;
string += `More or less ${crd.accuracy} meters.`;
alert(string)
}
function error(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
}
navigator.geolocation.getCurrentPosition(success, error, options);
};
onMount(loadWorker);
let latitude;
let longitude;
let accuracy;
export let data;
let divider;