rpsep2
rpsep2

Reputation: 3111

Ask for geolocation permission again, if it was denied

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

Answers (3)

Sergiu Mare
Sergiu Mare

Reputation: 1724

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.

Upvotes: 8

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

gtournie
gtournie

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

Related Questions