Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否有测试来确定页面是否请求位置授权?_Javascript_Location - Fatal编程技术网

Javascript 是否有测试来确定页面是否请求位置授权?

Javascript 是否有测试来确定页面是否请求位置授权?,javascript,location,Javascript,Location,如果用户的浏览器请求允许我们确定用户的位置,我想在页面上显示信息,但是如果浏览器已经通过身份验证,可以向我们提供其位置,我不想尝试显示此信息 有什么好办法吗 详细地说。我想显示一条消息,上面写着“请点击“允许这些人获取你的位置”的框”,因为在用户测试中,我们发现用户看不到对话,会感到困惑 但是,这会导致我们闪烁帮助消息,即使我们在他们通过位置查找页面时获得了他们的许可。存储他们给予我们许可的事实没有帮助,因为他们可能会撤销该许可,而我们不会知道 有什么想法吗 干杯 标记。您可以将错误回调交给ge

如果用户的浏览器请求允许我们确定用户的位置,我想在页面上显示信息,但是如果浏览器已经通过身份验证,可以向我们提供其位置,我不想尝试显示此信息

有什么好办法吗

详细地说。我想显示一条消息,上面写着“请点击“允许这些人获取你的位置”的框”,因为在用户测试中,我们发现用户看不到对话,会感到困惑

但是,这会导致我们闪烁帮助消息,即使我们在他们通过位置查找页面时获得了他们的许可。存储他们给予我们许可的事实没有帮助,因为他们可能会撤销该许可,而我们不会知道

有什么想法吗

干杯
标记。

您可以将错误回调交给
getCurrentPosition()
以确定用户是否拒绝跟踪/无法确定位置()

此外,我会设置一个超时,在一定时间后提示您的消息,因为在这种情况下,用户很可能忽略了浏览器对话框

示例代码:

function getLocation(){
  if (navigator.geolocation) {
     navigator.geolocation.getCurrentPosition(showPosition,showError);
     // set a Timeout, after which the user gets prompted
     // ugly global var but you could prevent this e.g. with a closure
     t = window.setTimeout(
              function(){alert("Please allow us to locate you;) !")},3000
         );
  } else {
     alert("Geolocation is not supported by this browser.");
  }
}
function showPosition(position){
      alert("Latitude: " + position.coords.latitude + " - Longitude: " + position.coords.longitude);
      // position could be determined, clear the timeout
      window.clearTimeout(t);   
}
function showError(error){
  // an error occured, clear the timeout as well
  window.clearTimeout(t);
  switch(error.code) {
    case error.PERMISSION_DENIED:
      alert("User denied the request.");
      // Do stuff here, etc. ask the user to please allow the request
      break;
    case error.POSITION_UNAVAILABLE:
      alert("No Location information available.");
      break;
    case error.TIMEOUT:
      // user probably didn't recognize the browser dialog
      alert("The request timed out.");
      break;
    case error.UNKNOWN_ERROR:
      alert("An unknown error occurred.");
      break;
    }
}

但是,您仍然可以存储权限并进行检查。如果他们撤销了他们的权限,您将遇到错误回调,在那里您可以采取相应的措施。

+1我也想知道。您是否尝试过调用
getCurrentPostion()
然后检查您是否获得了
权限\u被拒绝的
错误?非常感谢,这绝对是一个可行的解决方案。我希望做一些更直接的事情,而不是像怀孕一样暂停超时@正如我所提到的标记-没有太多问题,您可以存储用户的决定,并根据需要在处理程序中更改它。