ask for geolocation permission again if it was denied

Viewed 63091

im 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')
}
3 Answers

Two ways of doing this:

  1. If you have a version of Chrome bigger than 83.0.4103.97 then use the lock icon in the URL

enter image description here

  1. For older versions of Chrome the bellow code will work fine:

The bellow code only works on Chrome.

Steps:

  1. Open Chrome
  2. Open the console
  3. Copy in the console
       var allowGeoRecall = true;
       var countLocationAttempts = 0;
  1. Copy in the console the functions
    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;
    }
  1. Run the function in the console
    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.

This can be reset in Page Info which can be accessed by clicking the lock icon next to the URL and allowing Location

Related