Skip to content

Commit

Permalink
change to first load map with location of IP, update position if geol…
Browse files Browse the repository at this point in the history
…ocation is allowed
  • Loading branch information
Utesgui authored Feb 5, 2024
1 parent 6432e9a commit 7623ab2
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,21 @@ <h1 style="text-align: center;">I want a cat!</h1>
// Initialize the map
let map;

// Check if geolocation is supported by the browser

// First, find location from IP
findLocationFromIP();

// Then, if geolocation is supported, ask the user for their location
if (GEOLOCATION_SUPPORTED) {
navigator.geolocation.getCurrentPosition(initializeMap, handleGeolocationError);
} else {
console.error("Geolocation is not supported by this browser.");
findLocationFromIP();
navigator.geolocation.getCurrentPosition(updateMap, handleGeolocationError);
}

function updateMap(position) {
let center = normalizeCoordinates(position);
map.setView(center, 13);
findAnimalShelters(center);
}

function handleGeolocationError(error) {
console.error(error);
findLocationFromIP();
Expand Down Expand Up @@ -70,7 +77,7 @@ <h1 style="text-align: center;">I want a cat!</h1>
return `<a href="${url.startsWith('http') ? url : `https://${url}`}" target="_blank">${url}</a>`;
});
}

function initializeMap(position) {
let center = normalizeCoordinates(position);
map = L.map(MAP_ID).setView(center, 13);
Expand Down

0 comments on commit 7623ab2

Please sign in to comment.