Javascript html 导航器“用户拒绝地理定位”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11725594/
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
html navigator "User denied Geolocation"
提问by Sagi Mann
I have an issue with window.navigator, I'm getting error code 1, "User denied Geolocation" whenever I run the following code as a local html file:
我有 window.navigator 的问题,每当我将以下代码作为本地 html 文件运行时,我都会收到错误代码 1,“用户拒绝地理位置”:
navigator.geolocation.getCurrentPosition(function(position) {
console.log(position);
}, function(positionError) {
console.log(positionError);
});
The output is coming from the error function, positionError contains:
输出来自错误函数,positionError 包含:
code: 1
message: "User denied Geolocation"
This does not happen if the containing html is served from some server.
如果包含的 html 是从某个服务器提供的,则不会发生这种情况。
Is this expected? Is there some way to use navigator from a local html? I am trying to write some mobile app, but am also trying to avoid network whenever possible.
这是预期的吗?有什么方法可以从本地 html 中使用导航器吗?我正在尝试编写一些移动应用程序,但也尽可能避免使用网络。
thanks.
谢谢。
回答by marteljn
If you are using chrome, please have a look at the answer below:
如果您使用的是 chrome,请查看以下答案:
HTML 5 Geo Location Prompt in Chrome
It appears this is a security restriction for the file
protocol. Looks like you are going to need to host it locally from a server.
看来这是file
协议的安全限制。看起来您将需要从服务器本地托管它。
回答by Charlie Tupman
A simple response that worked for me was to simply change my url from
一个对我有用的简单响应是简单地将我的网址从
localhost
to
到
127.0.0.1
Chrome asked me to confirm I wanted to share my location settings and I was away!
Chrome 要求我确认我想分享我的位置设置,但我离开了!