24

I would like to hide() or show() a button that allows users to use their current location based on whether or not they are currently allowing location to be used in their browser setting. the below code only checks if the browser supports geolocation and not whether or not the particular user is allowing it.

if (navigator.geolocation)  {
   navigator.geolocation.getCurrentPosition(showPosition);
   } else  {
 x.innerHTML="Geolocation is not supported by this browser.";}
 } 

Is there a boolean value that I can detect for their browser setting letting me know if location is currently allowed?

thanks for any suggestions.

Rob Grzyb
  • 785
  • 14
  • 20
Tom
  • 537
  • 3
  • 7
  • 14

2 Answers2

45

Have you read http://www.w3schools.com/html/html5_geolocation.asp

What you want to do is check the errors to see if they allowed it or denied the request.

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition,showError);
  } else {
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
  x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;    
}

function showError(error) {
  switch(error.code) {
    case error.PERMISSION_DENIED:
      x.innerHTML = "User denied the request for Geolocation."
      break;
    case error.POSITION_UNAVAILABLE:
      x.innerHTML = "Location information is unavailable."
      break;
    case error.TIMEOUT:
      x.innerHTML = "The request to get user location timed out."
      break;
    case error.UNKNOWN_ERROR:
      x.innerHTML = "An unknown error occurred."
      break;
  }
}
DBS
  • 9,110
  • 4
  • 35
  • 53
Bot
  • 11,868
  • 11
  • 75
  • 131
  • 6
    GPS is supported in my browser but i have disabled it. In this case i get nothing. How should i detect if location is disabled or not? – Rahul Sharma Aug 30 '18 at 10:34
30

The below code will allow you to check the permission status without invoking the navigator.geolocation permission request.

Browsers Supported: Chrome(43+), Firefox(46+), Edge and Opera.

Unsupported: Safari(mac, ios), Internet explorer, Android webview.

    navigator.permissions && navigator.permissions.query({name: 'geolocation'})
    .then(function(PermissionStatus) {
        if (PermissionStatus.state == 'granted') {
              //allowed
        } else if (PermissionStatus.state == 'prompt') {
              // prompt - not yet grated or denied
        } else {
             //denied
        }
    })

Here is the Reference Link.

Compatibility on other browsers is unknown. I haven't tested it myself but please feel to test yourself and comment below.

Noushad
  • 6,063
  • 3
  • 25
  • 28
  • what about other browsers like IE/Safari/Opera? – Shivek Parmar Oct 27 '17 at 08:42
  • As of now, its is not supported in those browsers. I am updating the answer with a reference link. – Noushad Oct 27 '17 at 14:28
  • This answer allows to check for user permission status for the current site settings only. But it will return `'granted'` event if user has granted this checking permission for this site, but not for browser. See [this answer for possible solution](https://stackoverflow.com/a/65694912/6125971). – Nickensoul Mar 11 '21 at 12:21