Search code examples
javascriptreactjsnavigationgeolocation

React request location permissions if status denied


I need some help with one issue on react. The question is how can I enable my geolocation if status denied.

Screenshot

1

Example how to call or redirect to location settings


Solution

  • create a function to check the permission status and request permission if the status is denied

    function getLocation() {
      if (navigator.geolocation) {
        navigator.permissions.query({name:'geolocation'}).then(permissionStatus => {
          if (permissionStatus.state === 'denied') {
            alert('Please allow location access.');
            window.location.href = "app-settings:location";
          } else {
            navigator.geolocation.getCurrentPosition(success, error);
          }
        });
      } else {
        alert('Geolocation is not supported in your browser.');
      }
    }