Is there a way of detecting whether a user has already given permission to use navigator.geolocation?(有没有办法检测用户是否已经授予使用 navigator.geolocation 的权限?)
问题描述
除了第一次设置cookie之外,有没有办法检测用户是否已经允许navigator.geolocation返回浏览器的纬度/经度?
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?
此主题已在其他地方部分回答
根据 GeoLocation API – Chrome/Safari – 权限管理和视觉差异,Chrome 请求可撤销的一次性权限.我还没有读完这篇文章,但似乎权限存储并不是纯 Chrome 要做的事情.
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.
推荐答案
如何使用localStorage 应该被所有html5浏览器支持(支持geoLocation)
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());
这是 jsfiddle 如果您需要检查
这篇关于有没有办法检测用户是否已经授予使用 navigator.geolocation 的权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有没有办法检测用户是否已经授予使用 navigator.geolocation 的权限?
基础教程推荐
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01