检查位置设置在了浏览器的用户被关闭 [英] check if location setting has been turned off in users browser

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

问题描述

我想隐藏()或显示()按钮,可以让用户根据他们是否被允许目前在他们的浏览器设置被使用的位置使用他们的当前位置。
下面code仅当浏览器支持地理定位和检查不是是否特定用户是允许的。

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?

感谢您的任何建议。

推荐答案

你读过<一个href=\"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;
    }
  }

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

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