检查用户浏览器中的位置设置是否已关闭 [英] check if location setting has been turned off in users browser

查看:21
本文介绍了检查用户浏览器中的位置设置是否已关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想隐藏()或显示()一个按钮,允许用户根据他们当前是否允许在浏览器设置中使用位置来使用他们的当前位置.下面的代码只检查浏览器是否支持地理定位,而不是特定用户是否允许.

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?

感谢您的任何建议.

推荐答案

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

这篇关于检查用户浏览器中的位置设置是否已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆