如何获得的WebView中每隔一段时间更新(带FINE_ACCURACY)HTML5位置 [英] How to get HTML5 position in WebView updated at a regular interval (with FINE_ACCURACY)

查看:229
本文介绍了如何获得的WebView中每隔一段时间更新(带FINE_ACCURACY)HTML5位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个应用程序来跟踪我的手机的定期(不仅是第一次) navigator.geolocation.getCurrentPosition()在HTML5网页运行在运行在Android 2.1应用程序运行的WebKit的网页视图。

I'm developing an application to track my phone at regular intervals (not only the first time) with navigator.geolocation.getCurrentPosition() running in an HTML5 page running in a webkit webview running in an android 2.1 application.

我已经读过答复如下问题:

I already read responses to the following questions:

<一个href=\"http://stackoverflow.com/questions/2267513/using-navigator-geolocation-getcurrentposition-in-webview-on-android-2-0-phone\">Using navigator.geolocation.getCurrentPosition中的WebView在Android 2.0+(PhoneGap的相关)

<一href=\"http://stackoverflow.com/questions/2390641/android-using-html5-to-determine-geolocation-in-webview-with-javascript-api\">Android:使用HTML5来确定的WebView地理位置与JavaScript API第

地理位置不能上的WebView
负荷
<一href=\"http://stackoverflow.com/questions/6921094/android-webview-using-geolocationpermissions\">Android使用网页视图 GeolocationPermissions

Using navigator.geolocation.getCurrentPosition in WebView on Android 2.0+ (PhoneGap related)
Android: Using html5 to determine geolocation in webview with javascript api
Geolocation cannot be load on webview
Android WebView using GeolocationPermissions

我设法通过JavaScript方法的位置 navigator.geolocation.getCurrentPosition(),即使有一个非常糟糕的精度(1421米)。

I managed to get the position through the javascript method navigator.geolocation.getCurrentPosition(), even if with a very bad accuracy (1421 meters).

但真正的问题是:如果我称之为 getCurrentPosition()方法不止一次,它返回我始终不变的立场,并总是以同样的坏精度(1421米)。看来,这2个情况之一发生:

But the real problem is: if I call the getCurrentPosition() method more than once, it returns me always the same position and always with the same bad accuracy (1421 meters). It seems that one of these 2 cases occurs:


  1. 该设备不会从GPS传感器(精精度)的位置,但它通过AGPS得到它(从3G小区),导致返回相同的位置和准确性。 GPS传感器显然是对的,我已经在清单文件设置正确的权限:

  1. The device does not get the position from the GPS sensor (fine accuracy), but it gets it via AGPS (from the 3g cell) causing the same position and accuracy being returned. The GPS sensor is obviously on, and I've set the right privileges in the manifest file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


  • 该设备得到了第一的位置称之为getCurrentPosition后正常,但接下来的这个方法被调用时,该位置从一些GPS缓存返回,位置是不是真的从GPS传感器得到。

  • The device got the first position correctly after calling "getCurrentPosition", but the next time this method is called, the position is returned from some "GPS cache" and the position is not really got from the GPS sensor.

    有没有人成功地得到了在固定的时间间隔更新过的位置 getCurrentPosition() HTML5 Javascript方法?

    Has anyone successfully gotten the position updated at regular interval through the getCurrentPosition() HTML5 Javascript method?

    推荐答案

    我发现手机(至少在iPhone手机),通常需要三个检查,以得到精确的数据,仿佛它们是三角的位置。这个问题,因为你已经发现,就是以新呼叫getCurrentPosition()似乎开始盲,所以也没有得到任何更准确的(结果各不相同,当你正在运行在台式机/笔记本此功能)。

    I've found that phones (at least with iPhones) usually need three checks to get an accurate reading, almost as if they are triangulating the position. The problem, as you have discovered, is that each new call to getCurrentPosition() seems to start "blind", so it doesn't get any more accurate (results vary when you're running this function on a desktop/laptop).

    解决方案是 geolocation.watchPosition()。这个函数使用previous呼吁提高精确度。大约5秒钟后,它,因为它会得到(如果你不动),让您停机检查的准确。此外,它会停止检查它认为它不会得到任何更准确的任意时间。使用 window.setTimeout()来控制的时间间隔。

    The solution is geolocation.watchPosition(). That function uses previous calls to improve accuracy. After about five seconds, it's as accurate as it's going to get (if you're not moving), so you stop checking. Also, it will stop checking any time it thinks it won't get any more accurate. Use window.setTimeout() to control the interval.

    演示: http://jsfiddle.net/ThinkingStiff/yn3Bq/

    HTML

    <div id="result"></div>
    

    脚本:

    var latitude, longitude, accuracy;
    
    function setGeolocation() {
        var geolocation = window.navigator.geolocation.watchPosition( 
            function ( position ) {
                latitude = position.coords.latitude;
                longitude = position.coords.longitude;
                accuracy = position.coords.accuracy;
                document.getElementById( 'result' ).innerHTML += 
                      'lat: ' + latitude + ', '
                    + 'lng: ' + longitude + ', '
                    + 'accuracy: ' + accuracy + '<br />';
            }, function () { 
                /*error*/ 
            }, {
                maximumAge: 250, 
                enableHighAccuracy: true
            } 
        );
        window.setTimeout( function () {
                window.navigator.geolocation.clearWatch( geolocation ) 
            }, 
            5000 //stop checking after 5 seconds
        );
    };
    
    setGeolocation();
    
    window.setTimeout( function () {
            setGeolocation();
        }, 
        30000 //check every 30 seconds
    );
    

    输出:

    纬度:35.5830119,LNG:-124.4871223,精度:40

       纬度:35.5829974,LNG:-124.4871525,精度:30

    lat: 35.5830119, lng: -124.4871223, accuracy: 40
    lat: 35.5829974, lng: -124.4871525, accuracy: 30

    这篇关于如何获得的WebView中每隔一段时间更新(带FINE_ACCURACY)HTML5位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆