javascript 每次用户重新加载页面时,让 html5 地理定位请求许可
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16353909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make html5 geolocation ask permission every time user reloads the page
提问by Chuck Norris
Why we need that:
为什么我们需要它:
We have some page in our website which allows users to enter some data and search for another customers in their area. When user opened that page the popup message must be shown
我们的网站上有一些页面,允许用户输入一些数据并搜索他们所在地区的其他客户。当用户打开该页面时,必须显示弹出消息
http://foo.bar.comwould like to use your current location.
http://foo.bar.com想使用您当前的位置。
After that user can discard that and fill the location fields manually or accept and be automatically redirected to results page.
之后,用户可以放弃它并手动填写位置字段或接受并自动重定向到结果页面。
Problem:
问题:
The problem is that when user selects any of the mentioned options at first time, browser remembers it and don't ask it next time. So when user accept it, every time he open the search page it will automatically redirect him to results page withous asking anything.
问题是当用户第一次选择任何提到的选项时,浏览器会记住它并且下次不会询问它。因此,当用户接受它时,每次他打开搜索页面时,它都会自动将他重定向到结果页面,而无需询问任何内容。
Code:
代码:
We are using basic html5 geolocation code
我们正在使用基本的 html5 地理定位代码
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(LocationMethod);
}
Conclusion:
结论:
I'm pretty sure that it's impossible to change permission that are browser-native. But I hope that someone will give nice solution for this problem. Thanks.
我很确定不可能更改浏览器原生的权限。但我希望有人能为这个问题提供很好的解决方案。谢谢。
回答by samz
once a user clicks deny/allow the browser remembers this decision for the site.
一旦用户单击拒绝/允许,浏览器就会记住该站点的此决定。
the best way to avoid a deny is to only call getCurrentPosition(...) when the user clicked something that means he wants to use his location. and then he would probably be glad to share it :)
避免拒绝的最佳方法是仅在用户单击表示他想要使用其位置的内容时才调用 getCurrentPosition(...)。然后他可能会很乐意分享它:)
A good example is on foursquare site. on entering it offers you to go to the map to some city, and only there by clicking the button on the map of "use my location" it prompts for it.
一个很好的例子是在foursquare网站上。在输入它时,您可以转到某个城市的地图,只有在那里单击“使用我的位置”地图上的按钮,它才会提示它。
If you are denied the location you could also show an info message with "you have denied location service in the past for this site, do enable it do bla bla bla". and fill in the bla bla bla with whatever.
如果您的位置被拒绝,您还可以显示一条信息消息,“您过去曾拒绝过此站点的位置服务,请启用它 do bla bla bla”。并用任何东西填写bla bla bla。