0

I have the code

navigator.permissions.query({name:'geolocation'}).then(function(result) {
  if (result.state === 'granted') {
    getLocation();
  } else if (result.state === 'prompt') {
    console.log('не решено');
  } else{
    getLocation();
  }
  // Don't do anything if the permission was denied.
});

If user get permission to geolocation - load block, if not - in block I have a button "get permission to geolocation". Its work on android, but iOS not support navigator.permissions.query. How can I check permitions in iOS not used getCurrentPosition with page load?

1 Answers1

1

You can use navigator.geolocation to obtain a Gelocation object which has the method getCurrentPosition()

This method is used to access location information, and in doing so the browser will ask the user to grant permission.

The getCurrentPosition method wants at least one parameter which is a callback function that takes a GelocationPosition object as its sole input parameter. This callback function will be called only in the case the user grant permission, so you are able to know if the user has denied or not the permission.

Here it is an example:

navigator.geolocation.getCurrentPosition(
    position => {
        // this function is called only if the user grant permission so here you can handle the granted state
    }, 
    error => {
        // this function is called if the user denied permission or some other errors occurs
    } 
);

"position" is a GelocationPosition object which allows you to know also the coords, "error" is a GelocationPositionError, to know which error has occurred you can use error.message

Here some useful content.

Ole Pannier
  • 3,208
  • 9
  • 22
  • 33