本文介绍了Chrome 中的 HTML 5 地理位置提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始接触 HTML 5 并测试地理位置......到目前为止喜欢它.不过,我遇到了一些速度障碍……当我尝试获取我的地理位置时,chrome 会自动阻止页面获取我的位置.这不会发生在其他网站,例如以下网站:

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

我使用的脚本:

<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"这样的域".这就是我没有收到提示的原因吗?如果是这样,是否可以强制浏览器请求获取用户地理位置的权限,在我的场景中是否可行?

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?

推荐答案

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

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

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

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

如果您没有 Python,Ruby 中也有等效的模块,或者 Visual Web Developer Express 带有内置的本地 Web 服务器.

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.

这篇关于Chrome 中的 HTML 5 地理位置提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:22
查看更多