【发布时间】:2011-08-30 20:20:48
【问题描述】:
如果地理定位被拒绝,我需要 JavaScript 来显示手动输入。
我尝试过的:
Modernizr.geolocation
navigator.geolocation
两者都没有描述用户之前是否拒绝访问地理位置。
【问题讨论】:
-
接受的答案更改为@endless
标签: javascript html w3c-geolocation
如果地理定位被拒绝,我需要 JavaScript 来显示手动输入。
我尝试过的:
Modernizr.geolocation
navigator.geolocation
两者都没有描述用户之前是否拒绝访问地理位置。
【问题讨论】:
标签: javascript html w3c-geolocation
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 等中按预期工作。
【讨论】:
根据 W3C geolocation specification,您的 getCurrentPosition 调用可以返回成功回调和失败回调。但是,将针对发生的 any 错误调用您的失败回调,该错误是以下之一: (0) 未知; (1) 许可被拒绝; (2) 职位不可用;或 (3) 超时。 [Source: 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
}
}
【讨论】:
修复 Firefox 问题非常简单。在我的例子中,我将地理位置保存在 Javascript 上的一个名为 geolocation 的全局变量中。在使用这个变量之前,我只是检查是否未定义,如果是,我只是从 IP 获取地理位置。
在我的网站中,我第一次获取位置没有任何问题,但我在我的简短示例中看到,因为太快了,所以第一次没有时间获取地理位置。
无论如何这只是一个例子,你应该在每种情况下进行调整。
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:我没有足够的声誉来评论上述答案,所以我不得不创建一个新答案。对此感到抱歉。
【讨论】:
在不提示用户的情况下,您可以使用新的权限 api,这是可用的:
navigator.permissions.query({ name: 'geolocation' })
.then(console.log)
(仅适用于 Blink 和 Firefox)
【讨论】:
可能的值为prompt、granted 和denied
const permissionStatus = await navigator?.permissions?.query({name: 'geolocation'})
const hasPermission = permissionStatus?.state // Dynamic value
注意:这需要在
async函数中
如果要处理失败的情况,请在第二行添加可选的?? myDefaultValue。
【讨论】:
?
Inline Null Check的使用提示:@babel/plugin-proposal-optional-chaining
navigator.geolocation.watchPosition(function(position) {
console.log("i'm tracking you!");
},
function(error) {
if (error.code == error.PERMISSION_DENIED)
console.log("you denied me :-(");
});
【讨论】: