'How to ask permission for geolocation every time?
I would like to be asked every time for the permission to access to geolocation. How to do that? I thought to revoke the permission everytime and then ask it for again, but that doesn't work. According to documentation it has been deprecated.
navigator.permissions.query({name:'geolocation'}).then(function(result) {
if (result.state === 'granted') {
//to do things here
} else if (result.state === 'prompt') {
//ask for permission
} else if(result.state === 'denied'){
//keep asking for permission
}
report(result.state);
});
/////
if ('geolocation' in navigator) {
console.log('there is geolocation')
navigator.geolocation.getCurrentPosition((success) => {
console.log('success geo', success.coords)
}, (error) => {
console.log(error, 'error')
}, { maximumAge: 0});
} else{
alert('Please grant access to location')
}
}
From what I have been reading, it seems that once the user has given access to location, the browser remembers. That's why I added also maximumAge; I thought that adding that, I could have a mechanism to allow to ask for the geolocation permission every time the user loads a page.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|