ask for geolocation permission again if it was denied

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


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.