Chrome 中的 HTML 5 地理位置提示

发布于 2024-10-27 05:47:32 字数 1356 浏览 2 评论 0原文

刚刚开始接触 HTML 5 并测试地理位置……到目前为止还很喜欢。不过,我遇到了一点减速……当我尝试获取我的地理位置时,chrome 会自动阻止页面获取我的位置。在其他网站上不会发生这种情况,例如以下网站:

http://html5demos.com/geo

我的脚本使用:

<script type="text/javascript" JavaScript" SRC="geo.js"></script>   
<script type="text/javascript" JavaScript" SRC="Utility.js"></script> 
<script type="text/javascript" JavaScript" SRC="jquery.js"></script> 
<script type="text/javascript" JavaScript" SRC="modernizr.js"></script>  

function get_location() {

        if (geo_position_js.init()) {
            geo_position_js.getCurrentPosition(show_map, handle_error);
        }

    }
    function show_map(position) {
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;

        alert("lat:" + latitude + " long:" + longitude);


    }
    function handle_error(err) {
        alert(err.code);
        if (err.code == 1) {
            // user said no!
        }
    }

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(show_map, handle_error);
    } else {
        error('not supported');
    }

我正在从我的计算机上的本地目录中对此进行测试,因此实际上并不存在像“http://whatever.com/mytestpage.html”这样的“域”。这就是我没有收到提示的原因吗?如果是这样,是否可以强制浏览器请求权限以获取用户的地理位置?在我的场景中是否可能?

Just starting to get into HTML 5 and an testing out geo location...liking it so far. I am hitting a bit of a speed bump though...when I try to get my geo location, chrome automatically blocks the page from getting my location. This does not happen at other sites such as the site below:

http://html5demos.com/geo

The scripts I'm using:

<script type="text/javascript" JavaScript" SRC="geo.js"></script>   
<script type="text/javascript" JavaScript" SRC="Utility.js"></script> 
<script type="text/javascript" JavaScript" SRC="jquery.js"></script> 
<script type="text/javascript" JavaScript" SRC="modernizr.js"></script>  

function get_location() {

        if (geo_position_js.init()) {
            geo_position_js.getCurrentPosition(show_map, handle_error);
        }

    }
    function show_map(position) {
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;

        alert("lat:" + latitude + " long:" + longitude);


    }
    function handle_error(err) {
        alert(err.code);
        if (err.code == 1) {
            // user said no!
        }
    }

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(show_map, handle_error);
    } else {
        error('not supported');
    }

I am testing this out from a local directory on my machine, so there isn't really a "domain" like "http://whatever.com/mytestpage.html". Is this why I am not getting prompted? If so, is it possible to force the browswer to request permission to get the user's geo location and is it possible in my scenario?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

浮世清欢 2024-11-03 05:47:33

如果您在服务器后面托管,但仍然面临问题:
尝试将 localhost 更改为 127.0.0.1 例如 http://localhost:8080/ http://127.0.0.1:8080/

我面临的问题是我在 eclipse IDE (eclipse luna) 中使用 apache tomcat 提供站点服务。

为了进行健全性检查,我使用了 Remy Sharp 的演示:
https://github.com/remy/html5demos/blob/eae156ca2e35efbc648c381222fac20d821df494 /演示/geo.html

,尽管在服务器上托管代码,但对错误函数进行了细微调整后出现错误(仅在 Firefox 上工作,在 chrome 和 safari 上失败):

“用户拒绝地理定位”

我做了进行以下更改以获得更详细的错误消息:

function error(msg) {
  var s = document.querySelector('#status');
  msg = msg.message ? msg.message : msg; //add this line
  s.innerHTML = typeof msg == 'string' ? msg : "failed";
  s.className = 'fail';

  // console.log(arguments);
}

http://10.0.2.2:8080 :

“无法确定当前位置”

if you're hosting behind a server, and still facing issues:
try changing localhost to 127.0.0.1 e.g. http://localhost:8080/ to http://127.0.0.1:8080/

The issue I was facing was that I was serving a site using apache tomcat within an eclipse IDE (eclipse luna).

For my sanity check I was using Remy Sharp's demo:
https://github.com/remy/html5demos/blob/eae156ca2e35efbc648c381222fac20d821df494/demos/geo.html

and was getting the error after making minor tweaks to the error function despite hosting the code on the server (was only working on firefox and failing on chrome and safari):

"User denied Geolocation"

I made the following change to get more detailed error message:

function error(msg) {
  var s = document.querySelector('#status');
  msg = msg.message ? msg.message : msg; //add this line
  s.innerHTML = typeof msg == 'string' ? msg : "failed";
  s.className = 'fail';

  // console.log(arguments);
}

failing on internet explorer behind virtualbox IE10 on http://10.0.2.2:8080 :

"The current location cannot be determined"

黯淡〆 2024-11-03 05:47:33

要实现简单的解决方法,只需将 HTML 文件复制到某个云共享(例如 Dropbox),然后在浏览器中使用共享链接即可。简单的。

For an easy workaround, just copy the HTML file to some cloud share, such as Dropbox, and use the shared link in your browser. Easy.

∝单色的世界 2024-11-03 05:47:33

我在尝试 Gelocation API 时也遇到了这个问题。然后我通过 Visual Studio 启动 IIS Express,然后访问该页面,它在所有浏览器中都可以正常工作。

I too had this problem when i was trying out Gelocation API. I then started IIS express through visual studio and then accessed the page and It worked without any issue in all browsers.

凉栀 2024-11-03 05:47:33

检查 Google Chrome 设置并允许位置访问

Change your default location settings.
On your computer, open Chrome.
At the top right, click More Settings.
Under "Privacy and security," click Site settings.
Click Location.
Turn Ask before accessing on or off.

在我更改这些设置后,地理定位对我有用。

Check Google Chrome setting and permit location access

Change your default location settings.
On your computer, open Chrome.
At the top right, click More Settings.
Under "Privacy and security," click Site settings.
Click Location.
Turn Ask before accessing on or off.

After I changed those settings, Geolocation worked for me.

月牙弯弯 2024-11-03 05:47:32

Chrome 中对于使用 file:/// URI 中的地理定位存在某种安全限制,但不幸的是,它似乎没有记录任何错误来表明这一点。它将在本地网络服务器上运行。如果你安装了 python,请尝试在测试文件所在的目录中打开命令提示符并发出命令:

python -m SimpleHTTPServer

它应该在端口 8000 上启动一个 Web 服务器(可能是其他东西,但它会在控制台中告诉你哪个端口它正在监听),然后浏览到 http://localhost:8000/mytestpage.html

如果您不这样做没有 python,Ruby 中也有等效的模块,或者 Visual Web Developer Express 附带内置的本地 Web 服务器。

There's some sort of security restriction in place in Chrome for using geolocation from a file:/// URI, though unfortunately it doesn't seem to record any errors to indicate that. It will work from a local web server. If you have python installed try opening a command prompt in the directory where your test files are and issuing the command:

python -m SimpleHTTPServer

It should start up a web server on port 8000 (might be something else, but it'll tell you in the console what port it's listening on), then browse to http://localhost:8000/mytestpage.html

If you don't have python there are equivalent modules in Ruby, or Visual Web Developer Express comes with a built in local web server.

可可 2024-11-03 05:47:32

以上都没有帮助我。

经过一番研究后,我发现自 M50(2016 年 4 月)起 - Chrome现在需要安全源(例如 HTTPS)进行地理定位

不安全来源上已弃用的功能

主机“localhost”是特殊的,因为它“潜在安全”。如果部署到开发计算机,您在开发过程中可能不会看到错误。

None of the above helped me.

After a little research I found that as of M50 (April 2016) - Chrome now requires a secure origin (such as HTTPS) for Geolocation.

Deprecated Features on Insecure Origins

The host "localhost" is special b/c its "potentially secure". You may not see errors during development if you are deploying to your development machine.

月光色 2024-11-03 05:47:32

正如 robertc 的回答中已经提到的,Chrome 会阻止某些功能,例如本地文件的地理位置。设置自己的网络服务器的一个更简单的替代方法是使用参数启动 Chrome--allow-file-access-from-files。然后您就可以使用地理位置,前提是您没有在设置中将其关闭。

As already mentioned in the answer by robertc, Chrome blocks certain functionality, like the geo location with local files. An easier alternative to setting up an own web server would be to just start Chrome with the parameter --allow-file-access-from-files. Then you can use the geo location, provided you didn't turn it off in your settings.

爱冒险 2024-11-03 05:47:32

最简单的方法是单击地址栏左侧的区域并更改此处的位置设置。 设置位置选项

它甚至允许为 file:/// 在此处输入图像描述

The easiest way is to click on the area left to the address bar and change location settings there. It allows to set location options even for file:///

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文