While experimenting with the geolocation API, I encountered an issue where my page kept repeatedly asking for permission upon refresh. To work around this problem, I attempted to save my coordinate data to local storage but encountered difficulties in making it function as intended. Is there a method available to prompt for permission just once without these recurring requests?
const COORDINATION = "coords";
function saveCords(coordsOBJ){
localStorage.setItem(COORDINATION, JSON.stringify(coordsOBJ));
}
function handleGeoError(position){
console.log("Failed to find position");
}
function handleGeoSuccess(position){
const latitude = position.coords.latitude;
console.log(latitude);
const longitude = position.coords.longitude;
const coordsOBJ = {
latitude,
longitude
}
saveCords(coordsOBJ);
}
function askForCoords(){
navigator.geolocation.getCurrentPosition(handleGeoSuccess, handleGeoError);
}
function loadCoordinate(){
const loadedCords = localStorage.getItem("COORDINATION");
if(loadedCords === null) {
askForCoords();
}
}
function init(){
loadCoordinate();
}