From 5e6d30766182d6cd8c9f0691ace08c1c4f77b707 Mon Sep 17 00:00:00 2001 From: Klaus Dandl <76051010+Utesgui@users.noreply.github.com> Date: Mon, 5 Feb 2024 17:47:12 +0100 Subject: [PATCH] fix shelters not loading when permission for geo location si saved --- index.html | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/index.html b/index.html index 4513074..6d59be5 100644 --- a/index.html +++ b/index.html @@ -28,13 +28,33 @@

I want a cat!

// Initialize the map let map; - // First, find location from IP - findLocationFromIP(); + findLocationFromIP().then(() => { + // Then, if geolocation is supported, ask the user for their location + if (GEOLOCATION_SUPPORTED) { + navigator.geolocation.getCurrentPosition(updateMap, handleGeolocationError); + } + else { + handleIPGeolocationError(); + } + }); + + function findLocationFromIP() { + return new Promise((resolve, reject) => { + $.get(IP_GEOLOCATION_URL, (position) => { + initializeMap(position); + resolve(); + }).fail(() => { + handleIPGeolocationError(); + resolve(); + }); + }); + } - // Then, if geolocation is supported, ask the user for their location - if (GEOLOCATION_SUPPORTED) { - navigator.geolocation.getCurrentPosition(updateMap, handleGeolocationError); + function handleIPGeolocationError() { + console.error("Failed to retrieve location from IP address."); + const defaultLocation = [48.5734053, 13.4503279]; // Fallback to Passau in Bayern + initializeMap({ coords: { latitude: defaultLocation[0], longitude: defaultLocation[1] } }); } function updateMap(position) { @@ -42,22 +62,12 @@

I want a cat!

map.setView(center, 13); findAnimalShelters(center); } - + function handleGeolocationError(error) { console.error(error); findLocationFromIP(); } - function findLocationFromIP() { - $.get(IP_GEOLOCATION_URL, initializeMap) - .fail(handleIPGeolocationError); - } - - function handleIPGeolocationError() { - console.error("Failed to retrieve location from IP address."); - const defaultLocation = [48.5734053, 13.4503279]; // Fallback to Passau in Bayern - initializeMap({ coords: { latitude: defaultLocation[0], longitude: defaultLocation[1] } }); - } function normalizeCoordinates(position) { let center;