首页 > 解决方案 > Only block once for navigator.geolocation.getCurrentPoistion

问题描述

It seems the default behavior for:

navigator.geolocation.getCurrentPosition((position) => {
  // doSomethingWithPoistion
})

makes it so if it's denied. It is denied for all subsequent calls. Is there a way so that when the user hits block on the popup, it's only for this immediate interaction?

That is to say I'm using location services on load of a page. I also have a button that prompts the same functionality. The button doesn't work if the user denies locations services(just get another error callback).

Is there a way to make it so on load denials are only for this callback and doesn't set it to continue to deny on subsequent getCurrentPosition calls?

标签: javascript

解决方案


从我在 Chrome 浏览器中重新制定时所知道的情况来看,与其说是拒绝地理定位调用,不如说是阻止了地理定位调用。

当运行您在 Chrome 中提供的代码片段时,只需通过点击按钮退出权限弹出窗口X,我就可以在每次运行代码时触发权限提示。如果我点击Block按钮关闭权限弹出窗口,该网站将添加到我在 Chrome 中被阻止的网站列表中,并且不再请求许可。

下面链接的这个解决方案似乎是处理这种情况的更好方法之一。它建议您检查地理定位权限的当前状态,并在当前被拒绝时通知用户。

https://stackoverflow.com/a/42476603/6121503


推荐阅读