56

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')
}
2
  • Currently the answer is no as answered below. However, 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
    Commented Mar 30, 2020 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 developer.mozilla.org/en-US/docs/Web/API/Permissions_API/…
    – Wayne
    Commented Nov 16, 2022 at 17:54

3 Answers 3

49

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

4
  • 1
    Won't the instructions be browser dependent? Commented Sep 28, 2016 at 18:49
  • 4
    They are browser dependent, I just gave an example.
    – gtournie
    Commented Sep 28, 2016 at 21:48
  • 2
    Would be great to have all main browsers links!
    – sp00m
    Commented Dec 4, 2016 at 11:59
  • In 2020 this is possible. Please check my code above. Commented Mar 13, 2020 at 9:18
8

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.

11
  • 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
    Commented Mar 30, 2020 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. Commented Mar 30, 2020 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
    Commented Mar 30, 2020 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
    Commented Mar 30, 2020 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. Commented Mar 30, 2020 at 14:04
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.