isAuthorizedForPreciseLocation
The isAuthorizedForPreciseLocation
property is used to determine whether a user has granted permission for precise location access on a web application or mobile app.
With this property, you can check if the user has given consent to access their exact location coordinates, including latitude and longitude.
Here’s an example of how to use the isAuthorizedForPreciseLocation
property:
if ('geolocation' in navigator) {
navigator.permissions.query({ name: 'geolocation' }).then(function (result) {
if (result.state === 'granted') {
// Permission already granted, proceed with accessing precise location
// Your code here...
} else if (result.state === 'prompt') {
// Permission not granted yet, show a location access prompt to the user
// Your code here...
} else if (result.state === 'denied') {
// Permission denied, handle it accordingly (e.g., fall back to approximate location)
// Your code here...
}
});
} else {
// Geolocation not supported, handle it accordingly
// Your code here...
}
In the example above, we first check if the Geolocation API is supported by the browser. If it is, we use the navigator.permissions.query()
method to request the permission status for geolocation access.
The result.state
property of the PermissionStatus
object indicates the current permission status. If it is 'granted'
, the user has already granted permission, and you can proceed with accessing their precise location. If it is 'prompt'
, the user has not granted or denied permission yet, and you can show a prompt to request their consent. If it is 'denied'
, the user has explicitly denied permission, and you should handle it accordingly.
It’s important to note that the user’s decision may change over time. You can listen for changes in the permission status by adding an event listener to the PermissionStatus
object:
result.onchange = function () {
console.log('Permission state changed to ' + this.state);
// Handle permission state changes
// Your code here...
};
By monitoring the permission state changes, you can adapt your application’s behavior accordingly.