54

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')
}
gre_gor
  • 6,669
  • 9
  • 47
  • 52
rpsep2
  • 3,061
  • 10
  • 39
  • 52
  • Currently the answer is no as answered below. However, [Permissions API](https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API) have it, among other APIs, but not all APIs currently. But there is a bit poor browser support right now, especially for the `request` method to ask for permission of user for suing a specific Web API. In addition, if request method could accept multiple api permissions and could ask user only once with a default UX asking Accept All alongside individual Accept button it would be convenient. – sçuçu Mar 30 '20 at 11:05
  • I found this link to webmd, but I can't get it to work. They seem to say that it can be done using revoke https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API/Using_the_Permissions_API – Wayne Nov 16 '22 at 17:54

3 Answers3

48

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).

gtournie
  • 4,143
  • 1
  • 21
  • 22
7

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.

Sergiu Mare
  • 1,552
  • 15
  • 17
  • Works on Chrome but not on Firefox. It runs endlessly without opening the standard prompt on Firefox. Do you have a suggestion for it? – sçuçu Mar 30 '20 at 12:17
  • 1
    Firefox, Edge, Opera, etc doesn't allow it, unfortunately. Chrome is the only browser that allows this behavior. There is nothing that you can do. The only thing you can do is before asking for this to make sure that the client knows why you need his location, otherwise, it's a pain to change it. – Sergiu Mare Mar 30 '20 at 12:28
  • Even googles case is a workaround. But glad to see it is possible somehiw in some browser. I think we really need a robust permissions api that is supported in browsers like permissions api i mentioned in my above comment with all methods supported. – sçuçu Mar 30 '20 at 13:39
  • Another question mark and suggestion: If we somehow absolutely need the location to make app usable and user still denies permission and we find a way to exit application on user deny, will this choice be remembered when the app lauched again or we can ask for location permission all over again. – sçuçu Mar 30 '20 at 13:45
  • 1
    Indeed it is, but because chrome sustains progressive web app it changed its behaviour compared with other browsers. In order to compete with native apps, we do need better APIs. Except for Chrome, all the browser will remember the user's decision regarding the geolocation question. In order to change it as a user you'll will have to dive into the browsers settings. – Sergiu Mare Mar 30 '20 at 14:04
  • It is strange. It was working before now it is not working. I do not know how it was working on Chrome before. We definitely need a robust Permissons API. Since it lands I am looking for other app development technologies. Web is not ready for me in that matter. – sçuçu Mar 30 '20 at 19:04
  • Downgrade your version @Oliver and it will work. At the time when I posted it worked fine. – Sergiu Mare Jun 12 '20 at 09:11
  • this is unending loop – Jenuel Ganawed Jun 08 '21 at 09:03
  • It's also no longer working in the latest Chrome - if user blocked it - it will go into an endless loop – Andrew Bogdanov Oct 21 '21 at 17:48
  • Made a small change to the loop issue so it stops after 5 tries. Stackoverflow doesn't offer solutions which are matching 100% on your codebase. You should adapt them to your needs. @JenuelGanawed – Sergiu Mare Oct 22 '21 at 15:30
  • @AndrewBogdanov Made a small change to the loop issue so it stops after 5 tries. Stackoverflow doesn't offer solutions which are matching 100% on your codebase. You should adapt them to your needs. – Sergiu Mare Oct 22 '21 at 15:32
2

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