I have a navigator.permissions.query that is not working

function handlePermission() {
  navigator.permissions.query({name:'geolocation'}).then(function(result) {
    if (result.state == 'granted') {
      report(result.state);
      geoBtn.style.display = 'none';
    } else if (result.state == 'prompt') {
      report(result.state);
      geoBtn.style.display = 'none';
      navigator.geolocation.getCurrentPosition(revealPosition,positionDenied,geoSettings);
    } else if (result.state == 'denied') {
      report(result.state);
      geoBtn.style.display = 'inline';
    }
    result.onchange = function() {
      report(result.state);
    }
  });
}

function report(state) {
  document.getElementById("t").innerHTML = ('Permission ' + state);
}

handlePermission();

I have a live example here please check out my code and tell me where I am going wrong

1 Answers

0
BreakBB On

You should read the example again and the docs for the functions you're using. The error messages in you live example are pretty clear.

navigator.geolocation.getCurrentPosition(revealPosition,positionDenied,geoSettings);

You have to hand the correct/valid parameters to this function. revealPosition is a function which is run, when the user granted the geolocation, positionDenied is a function which is run, when the user denies the access to the geolocation and geoSettings is an object which includes the setting for the call to .getCurrentPosition.

So once you create the missing parameters instead of handing undefined references to the call the function will run just fine.