Reputation: 3111
I'm building an app through phonegap, with a geolocation button.
If a user denies permission for geolocation the first time, how can I ask for permission again, when they click the geolocation button again?
My code structure at the moment is:
function getLocation() {
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, positionError);
} else {
hideLoadingDiv()
showError('Geolocation is not supported by this device')
}
}
function positionError() {
hideLoadingDiv()
showError('Geolocation is not enabled. Please enable to use this feature')
}
Upvotes: 56
Views: 84989
Reputation: 1724
Two ways of doing this:
The bellow code only works on Chrome.
Steps:
var allowGeoRecall = true;
var countLocationAttempts = 0;
function getLocation() {
console.log('getLocation was called')
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition,
positionError);
} else {
hideLoadingDiv()
console.log('Geolocation is not supported by this device')
}
}
function positionError() {
console.log('Geolocation is not enabled. Please enable to use this feature')
if(allowGeoRecall && countLocationAttempts < 5) {
countLocationAttempts += 1;
getLocation();
}
}
function showPosition(){
console.log('posititon accepted')
allowGeoRecall = false;
}
getLocation();
After running this you will be asked to allow to share your position. If your response is negative you will be asked again until you agree.
HINT: If your user has a negative response, let him know why you need the coordinates. Is vital for him to understand that this step is vital for the good run of the web app.
Upvotes: 8
Reputation: 29
This can be reset in Page Info which can be accessed by clicking the lock icon next to the URL and allowing Location
Upvotes: 2
Reputation: 4392
You can't.
The only thing you can do is to display the instructions to reactivate the location sharing in his browser's settings (https://support.google.com/chrome/answer/142065?hl=en).
Upvotes: 49