Skip to content

Commit

Permalink
fix shelters not loading when permission for geo location si saved
Browse files Browse the repository at this point in the history
  • Loading branch information
Utesgui authored Feb 5, 2024
1 parent 7623ab2 commit 5e6d307
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,46 @@ <h1 style="text-align: center;">I want a cat!</h1>
// 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) {
let center = normalizeCoordinates(position);
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;
Expand Down

0 comments on commit 5e6d307

Please sign in to comment.