navigator.geolocation.getCurrentPosition不适用于Android谷歌浏览器

And*_*sov 32 javascript html5 android google-chrome geolocation

这段代码:

navigator.geolocation.getCurrentPosition(
                    function(position) {
                        alert(position.coords.latitude, position.coords.longitude);
                    },
                    function(error){
                        alert(error.message);
                    }, {
                        enableHighAccuracy: true
                        ,timeout : 5000
                    }
            );
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/FcRpM/在我的笔记本电脑上使用谷歌Chrome浏览器,但在移动HTC one S(安卓4.1,GPS关闭,通过移动网络和启用wifi的位置),通过WiFi连接到互联网.

  1. 默认浏览器工作正常.
  2. Google Chrome,Opera,Yandex.browser for android因"Timeout expired"而失败.

其他Android应用程序找到我正确.

Adr*_*ian 17

你可以试试这个.它似乎适用于我的设备(三星Galaxy Nexus在Wi-Fi上运行Chrome 27.0.1453.90(没有数据连接,没有GPS))

navigator.geolocation.getCurrentPosition(
    function(position) {
         alert("Lat: " + position.coords.latitude + "\nLon: " + position.coords.longitude);
    },
    function(error){
         alert(error.message);
    }, {
         enableHighAccuracy: true
              ,timeout : 5000
    }
);
Run Code Online (Sandbox Code Playgroud)

问题是警报只接受字符串(以原始形式)但是你传递了2个双打.例如,将警报框修改为alert('Hey', 'Hello');仅输出Hey.更改,+,您将获得连接的字符串HeyHello.您不能在其中使用+符号,alert因为方程式将首先执行然后显示.

希望这说清楚.

  • 似乎能够实现高精度,这对我来说是固定的.注意到如果没有将此选项设置为true,它根本没有击中GPS. (4认同)
  • 问题不在 alert('Hey', 'Hello'); 我看到一行,该代码中的问题不返回位置。您的代码显示“超时已过期”警报 HTC One S Chrome Chrome 27.0.1453.90 (2认同)

del*_*ant 8

有一个替代方法:watchPosition调用,并在清除watchID之前等待5秒钟.代码如下;

var options = { enableHighAccuracy: true, maximumAge: 100, timeout: 60000 };
if( navigator.geolocation) {
   var watchID = navigator.geolocation.watchPosition( gotPos, gotErr, options );
   var timeout = setTimeout( function() { navigator.geolocation.clearWatch( watchID ); }, 5000 );
} else {
   gotErr();
}
Run Code Online (Sandbox Code Playgroud)

我没有玩过"选项"值或此刻的超时延迟,但上面的代码在我尝试的每个平台上都带回了准确的定位信息.

  • 你确定解决方法有效吗?我只是尝试过,但事实并非如此 (2认同)

kar*_*nen 6

刚完成测试一堆移动设备和Javascript Geolocation.我使用了Google的示例代码,以确保问题不在我自己的代码中.

Chrome for Android 4.4似乎不适用于仅限GPS的定位服务,2.3版本的浏览器也不适用.他们都需要"高精度" - 使用无线和3G/4G网络.

有趣的是,Firefox for Android可以毫无问题地使用GPS.只有现有的Android浏览器(Chrome + Mobile Safari)才能使用仅限GPS的位置设置.

其余的团伙 - 带有GPS,Windows和Linux(都带有ADSL)的Lumia WP8可以与任何位置设置完美配合.