fixed getCurrentLocation

This commit is contained in:
Pete Gerlach
2024-06-11 14:14:49 +02:00
parent 1e0bf3f915
commit 34f95c07ac

View File

@@ -1,13 +1,25 @@
export const locationUtils = { export const locationUtils = {
getCurrentLocation: () => { async getCurrentLocation () {
if (navigator.geolocation) { let location = await this.returnCurrentLocation().catch((error) => {
navigator.geolocation.getCurrentPosition(async function(position) { console.error('Error getting current location', error);
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
return {lon: longitude, lat: latitude};
}); });
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 { } else {
return {lon: null, lat: null}; reject(new Error('Geolocation is not supported by this browser.'));
} }
});
} }
} }