Javascript 检查用户浏览器中的位置设置是否已关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14862019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 18:10:01  来源:igfitidea点击:

check if location setting has been turned off in users browser

javascriptjquerybrowsergeolocation

提问by Tom

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.

感谢您的任何建议。

回答by Bot

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

你读过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;
  }
}

回答by Noushad

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

下面的代码将允许您在不调用navigator.geolocation权限请求的情况下检查权限状态。

Browsers Supported: Chrome, Firefox, Edge and Opera.

支持的浏览器:Chrome、Firefox、Edge 和 Opera。

Unsupported: Safari, Internet explorer.

不支持:Safari、Internet Explorer。

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.

其他浏览器的兼容性未知。我自己还没有测试过,但请自行测试并在下面发表评论。