I have an html, css, js project with a map using leaflet. Sometimes location works but most of the time I get errors. Here is my JS code:
onst map = L.map('map').setView([30, 0], 3);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 19, attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'}).addTo(map);
const err = document.querySelector("#error");
const moodIcons = document.querySelectorAll(".mood-icon");
let currentLocation = null;
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error, { maximumAge: 5000, timeout: 10000, enableHighAccuracy: true });
}
else {
err.innerHTML = "Geolocation is not supported by this browser.";
}
return location;
}
function success(position) {
currentLocation = [position.coords.latitude, position.coords.longitude]
map.setView(currentLocation, 17);
}
function error(error) {
console.log(error)
err.innerHTML = "Please allow location permission to add a marker to the mood map";
}
// ---------------------------------------------------------------------------------------------
getLocation();
On Firefox I get this error:
On Chrome I get this error:
When I open my html page in the browser I get prompted to allow location. When I click on allow Iocation permission the console immediately shows the error. have tried setting options for the getCurrentPosition method like a timeout but issue persists. The weird thing is that sometimes it will work. Without me even changing the code. Additionally, I get this error on Chrome only 127.0.0.1/:1 Network location provider at 'https://www.googleapis.com/' : Returned error code 429.
UPDATE: I stopped working on this code for 2 days after I posted this question. I made no changes to the code at all. I came back to it and it was working perfectly fine. The next day I went to work on it and it doesn't work even when the code was not changed. What could be causing this behavior?

