Files
SWA-frontend/src/utils/location.js
2024-06-11 14:14:49 +02:00

25 lines
921 B
JavaScript

export const locationUtils = {
async getCurrentLocation () {
let location = await this.returnCurrentLocation().catch((error) => {
console.error('Error getting current location', error);
});
return {lon: location.longitude, lat: location.latitude};
},
async returnCurrentLocation() {
return new Promise((resolve, reject) => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(position) => {
const { latitude, longitude } = position.coords;
resolve({ latitude, longitude });
},
(error) => {
reject(error);
}
);
} else {
reject(new Error('Geolocation is not supported by this browser.'));
}
});
}
}