是否有一种方法来检测用户是否已经授予使用navigator.geolocation的权限? [英] Is there a way of detecting whether a user has already given permission to use navigator.geolocation?

查看:921
本文介绍了是否有一种方法来检测用户是否已经授予使用navigator.geolocation的权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了第一次设置cookie,是否有一种方法来检测用户是否已经给navigator.geolocation赋予了返回浏览器的纬度/经度的权限?



如果有,它是什么,它是在所有浏览器或不同的所有浏览器相同?



此主题已在其他地方部分回答

根据 GeoLocation API - Chrome / Safari - 权限管理和视觉差异,Chrome会要求提供可撤销的一次性权限。我还没有完成阅读这篇文章,但看起来存储的权限不是一个纯粹的Chrome做的事情。

解决方案

p>如何使用localStorage,应该支持所有html5浏览器(支持geoLocation)

  if(navigator.geolocation){ 
navigator.geolocation.getCurrentPosition(successFunction,errorFunction);
}
//获取纬度和经度;
function successFunction(position){
var lat = position.coords.latitude;
var long = position.coords.longitude;

localStorage ['authorizedGeoLocation'] = 1;
}

function errorFunction(){
localStorage ['authorizedGeoLocation'] = 0;
}

function checkauthorizedGeoLocation(){//你可以使用这个函数知道geoLocation是否被允许
if(typeof localStorage ['authorizedGeoLocation'] ==undefined || localStorage ['authorizedGeoLocation'] ==0)
return false;
else
return true;
}

然后使用以下函数进行检查:

  alert(checkauthorizedGeoLocation()); 

这是jsfiddle如果您需要检查


Apart from setting a cookie the first time round, is there a way of detecting whether a user has already given permission for navigator.geolocation to return the lat/long of the browser?

If there is, what is it and is it the same across all browsers or different across all browsers?

This subject has been partially answered elsewhere

According to GeoLocation API – Chrome / Safari – Permission management and Visual Differences, Chrome asks for a revokable one-time permission. I haven't finished reading the article, but it would seem that storage of permissions is not a purely-Chrome thing to do.

解决方案

What about using localStorage which should be supported by all html5 browsers (that support geoLocation)

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
} 
//Get latitude and longitude;
function successFunction(position) {
    var lat = position.coords.latitude;
    var long = position.coords.longitude;

    localStorage['authorizedGeoLocation'] = 1;
}

function errorFunction(){
    localStorage['authorizedGeoLocation'] = 0;
}

function checkauthorizedGeoLocation(){ // you can use this function to know if geoLocation was previously allowed
    if(typeof localStorage['authorizedGeoLocation'] == "undefined" || localStorage['authorizedGeoLocation'] == "0" ) 
        return false;
    else 
        return true;
}

And then you check using the below function :

alert(checkauthorizedGeoLocation());

This is the jsfiddle if you need to check

这篇关于是否有一种方法来检测用户是否已经授予使用navigator.geolocation的权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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