From 34f95c07acfd0ef815ab7116fe560407165a5d1a Mon Sep 17 00:00:00 2001 From: Pete Gerlach Date: Tue, 11 Jun 2024 14:14:49 +0200 Subject: [PATCH] fixed getCurrentLocation --- src/utils/location.js | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/utils/location.js b/src/utils/location.js index 1bd25c5..0a40db8 100644 --- a/src/utils/location.js +++ b/src/utils/location.js @@ -1,13 +1,25 @@ export const locationUtils = { - getCurrentLocation: () => { - if (navigator.geolocation) { - navigator.geolocation.getCurrentPosition(async function(position) { - const latitude = position.coords.latitude; - const longitude = position.coords.longitude; - return {lon: longitude, lat: latitude}; - }); - } else { - return {lon: null, lat: null}; - } + 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.')); + } + }); } } \ No newline at end of file