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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:51:25  来源:igfitidea点击:

html navigator "User denied Geolocation"

javascripthtmlgeolocationnavigator

提问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

Chrome 中的 HTML 5 地理位置提示

It appears this is a security restriction for the fileprotocol. 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 要求我确认我想分享我的位置设置,但我离开了!

回答by Sumeet

Just click on the left side of the url on the little 'i' symbol:- (Refer the image below)

只需单击 url 左侧的小“i”符号:-(参考下图)

info symbol illustration

信息符号图

Then check if the location is 'block'. Change it to 'allow' and then reload the page.

然后检查该位置是否为“阻止”。将其更改为“允许”,然后重新加载页面。

Hope this helps.

希望这可以帮助。