如果地理定位被拒绝,我需要 JavaScript 来显示手动输入。
我试过的:
Modernizr.geolocation
navigator.geolocation
两者都没有描述用户之前是否拒绝访问地理位置。
如果地理定位被拒绝,我需要 JavaScript 来显示手动输入。
我试过的:
Modernizr.geolocation
navigator.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 等 中按预期工作。
在不提示用户的情况下,您可以使用新的权限 api,如下所示:
navigator.permissions.query({ name: 'geolocation' })
.then(console.log)
(仅适用于 Blink 和 Firefox)
根据 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
}
}
可能的值为prompt
、granted
和denied
const permissionStatus = await navigator?.permissions?.query({name: 'geolocation'})
const hasPermission = permissionStatus?.state // Dynamic value
注意:这需要在一个
async
函数中
如果要处理失败的情况,?? myDefaultValue
请在第二行添加一个可选项。
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.
navigator.geolocation.watchPosition(function(position) {
console.log("i'm tracking you!");
},
function(error) {
if (error.code == error.PERMISSION_DENIED)
console.log("you denied me :-(");
});