80

如果地理定位被拒绝,我需要 JavaScript 来显示手动输入。

我试过的:

Modernizr.geolocation
navigator.geolocation

两者都没有描述用户之前是否拒绝访问地理位置。

4

6 回答 6

140

watchPosition并且getCurrentPosition两者都接受第二个回调,当出现错误时调用该回调。错误回调为错误对象提供参数。对于权限被拒绝,error.code将是error.PERMISSION_DENIED(数值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 等 中按预期工作。

于 2011-05-23T02:57:41.857 回答
53

在不提示用户的情况下,您可以使用新的权限 api,如下所示:

navigator.permissions.query({ name: 'geolocation' })
.then(console.log)

(仅适用于 Blink 和 Firefox)

http://caniuse.com/#feat=permissions-api

于 2016-06-10T13:58:40.313 回答
18

根据 W3C地理定位规范,您的getCurrentPosition调用可以返回成功回调和失败回调。但是,将针对发生的任何错误调用您的失败回调,该错误是以下之一: (0) 未知;(1) 许可被拒绝;(2) 职位不可用;或 (3) 超时。[来源:Mozilla ]

在您的情况下,如果用户明确拒绝访问,您想要做一些特定的事情。您可以检查error.code失败回调中的值,如下所示:

navigator.geolocation.getCurrentPosition(successCallback,
    errorCallback,
    {
        maximumAge: Infinity,
        timeout:0
    }
);

function errorCallback(error) {
    if (error.code == error.PERMISSION_DENIED) {
        // pop up dialog asking for location
    }
}
于 2011-05-23T03:07:55.833 回答
6

内联权限检查

可能的值为promptgranteddenied

const permissionStatus = await navigator?.permissions?.query({name: 'geolocation'})
const hasPermission = permissionStatus?.state // Dynamic value

注意:这需要在一个async函数中

默认值:

如果要处理失败的情况,?? myDefaultValue请在第二行添加一个可选项。

链接:

于 2020-08-23T19:31:06.823 回答
3

To fix the Firefox problem is really easy. In my case I save the geolocation in a global variable on Javascript called geolocation. Before use this variable, I just check if is not undefined, if so I just get the geolocation from the IP.

In my website I don't have any problem getting the location the first time, but I saw in my short example that never has time to get the geolocation on the first time because is too quick.

Anyway this is just an example, you should adapt it in each case.

var geolocation = {};
getLocation();

$(document).ready(function(){
    printLocation(); // First time, hasn't time to get the location
});

function printLocation(){
    if(typeof geolocation.lat === "undefined" || typeof geolocation.long === "undefined"){
        console.log("We cannot get the geolocation (too fast? user/browser blocked it?)");
        // Get location by IP or simply do nothing
    }
    else{
        console.log("LATITUDE => "+geolocation.lat);
        console.log("LONGITUDE => "+geolocation.long);
    }
}

function getLocation() {
    // If the user allow us to get the location from the browser
    if(window.location.protocol == "https:" && navigator.geolocation)
        navigator.geolocation.getCurrentPosition(function(position){
            geolocation["lat"] = position.coords.latitude;
            geolocation["long"] = position.coords.longitude;
            printLocation(); // Second time, will be return the location correctly
        });
    else{
        // We cannot access to the geolocation
    }
}

PS: I don't have enough reputation to comment the above answers so I had to create a new answer. Sorry about that.

于 2016-06-10T12:20:39.280 回答
0

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

于 2021-12-23T12:11:24.693 回答