check if location setting has been turned off in users browser

梦想的初衷 提交于 2019-11-27 19:51:34
Bot

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;
  }
}

The below code will allow you to check the permission status without invoking the navigator.geolocation permission request on Chrome 43+ and Firefox 46+.

navigator.permissions && navigator.permissions.query({name: 'geolocation'}).then(function(PermissionStatus) {
    if(PermissionStatus.state == 'granted'){
          //allowed
    }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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!