有没有一种方法来检查地理位置是否已经用Javascript降低了? [英] Is there a way to check if geolocation has been DECLINED with Javascript?

查看:201
本文介绍了有没有一种方法来检查地理位置是否已经用Javascript降低了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果地理位置被拒绝,我需要JavaScript来显示手动条目。

I need JavaScript to display a manual entry if geolocation is declined.

我尝试过:

Modernizr.geolocation
navigator.geolocation

描述用户以前是否拒绝过对地理定位的访问。

Neither describes if user has previously declined access to geolocation.

推荐答案

watchPosition getCurrentPosition 都接受第二个回调,当有错误时调用它。错误回调为错误对象提供了一个参数。对于拒绝的权限, error.code 将会是 error.PERMISSION_DENIED (数值 1 < code $)。

watchPosition and getCurrentPosition both accept a second callback which is invoked when there is an error. The error callback provides an argument for an error object. For permission denied, error.code would be error.PERMISSION_DENIED (numeric value 1).

在这里阅读更多: https://developer.mozilla.org/en/Using_geolocation

示例:

navigator.geolocation.watchPosition(function(position) {
  console.log("i'm tracking you!");
},
function (error) { 
  if (error.code == error.PERMISSION_DENIED)
      console.log("you denied me :-(");
});

编辑:正如@Ian Devlin指出的那样,似乎Firefox(本文发表时为4.0.1)支持这一点行为,它在Chrome和中可能正常运行 Safari等。

As @Ian Devlin pointed out, it doesn't seem Firefox (4.0.1 at the time of this post) supports this behavior. It works as expected in Chrome and probably Safari etc.

这篇关于有没有一种方法来检查地理位置是否已经用Javascript降低了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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