地理位置监视无法持续启动 [英] Geolocation watch doesn't fire consistently

查看:138
本文介绍了地理位置监视无法持续启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试应用程序,可从HTML5地理位置服务中获取经/纬度值.不幸的是,它以非常不一致的时间间隔触发,从500毫秒到10000毫秒不等.我尝试更改手表的maximimAge和timeout参数,但这些参数似乎没有任何更改.我正在Chrome中以及通过Android Lollipop构建中的简单Cordova应用进行测试.下面的代码仅显示手表的时间戳值,以消除可能导致此问题的其他任何延迟.似乎该间隔接近1秒然后5秒的重复模式.我还尝试过将地理位置提取功能放置在setInterval函数中,并且其行为具有相同的1和5秒重复间隔.

I've got a test app that fetches lat/lng values from the HTML5 geolocation service. Unfortunately, it's firing at rather inconsistent time intervals, anywhere from 500ms to 10000ms. I've tried changing the maximimAge and timeout parameters for the watch but those don't seem to change anything. I'm testing it in Chrome as well as via a simple Cordova app on an Android Lollipop build. The code below simply displays the timestamp value of the watch to eliminate any other delays that could be causing the issue. It appears that the interval is close to a 1 second then 5 second repeating pattern. I've also tried placing the geolocation fetch function inside a setInterval function and it behaves with the same 1 and 5 second repeating interval.

<html>
<body>
    <h1>Timestamp</h1>
    <div id="mytimestamp"></div>
    <button id="btnstopwatch">Stop GPS</button>
</body>
</html>

<script type="text/javascript">
    var mytimestamp = document.getElementById("mytimestamp");
    //start watching location
    watchPositionId = navigator.geolocation.watchPosition(updateCompass,handleerror,{
        enableHighAccuracy:true, 
        maximumAge:3000, 
        timeout:3000
    });

function updateCompass(p)
{
    mytimestamp.innerHTML = p.timestamp;
}

function handleerror(err)
{
    if(err.code ==1)
    {
        //user said no
        alert('Please allow access to GPS');
    }
    else
    {
        alert(err.code);
    }
}
</script>

推荐答案

与watchPosition()有关,位于以下链接 w3说:"仅当获得新职位时才调用successCallback .......实现可能会限制回调的频率,因此以避免无意中消耗不成比例的资源."

Relating to watchPosition(), on the following link w3 says "the successCallback is only invoked when a new position is obtained .......implementations may impose limitations on the frequency of callbacks so as to avoid inadvertently consuming a disproportionate amount of resources."

这意味着watchPosition()正在等待事件,这些事件将导致位置发生变化,否则将不执行任何操作.因此,除非看到位置发生变化,否则watchPosition()将不会调用updateCompass(). 另外,关于watchPosition(), maximumAge timeout 参数与获取新位置有关,这些参数与watchPosition()的次数和时间无关.被称为...

What it means is that watchPosition() is waiting for events which will cause changes in position to happen, otherwise it does nothing. So watchPosition() will not be calling updateCompass() unless it sees the changes in position. Also, in regard watchPosition(), maximumAge and timeout parameters are related to the acquiring new position, and these parameters has nothing to do with how much times and when watchPosition() is called...

WatchPosition()防止连续调用以查看位置是否改变,并且仅在发生某些与位置改变有关的事件时才作出反应,这有助于节省电池等资源.但是,如果您真的想查看位置,可以说每三秒钟一次,无论位置是否更改,我都认为设置间隔是个好方法.

WatchPosition() prevents continuous calls to see if position changes or not, and reacts only if some events related to position changes occur, this help to save resources like battery. But if you really want to see the position, let's say every three second, no matter if position changed or not I think setting an interval will be good approach.

var watchPositionId;
   //start watching location
    setInterval(function () {
    watchPositionId = navigator.geolocation.watchPosition(updateCompass, handleerror, {
        enableHighAccuracy: true,
        maximumAge: 3000,
        timeout: 3000
    });
    }, 3000);

    function updateCompass(p) {
        mytimestamp.innerHTML = p.timestamp;
        // if you have alert message it will counts seconds once you dismiss the alert,
        //this may be part of the issue why you kept getting diffrent intervals.
        //alert(p.timestamp);
        navigator.geolocation.clearWatch(watchPositionId);
    }

您提到您尝试设置setInterval并没有用,所以请注意,我在上面的代码中注释了alert(),因为它导致我有延迟,如果没有它,则html中的时间戳会每隔3000毫秒更新一次,精度约5ms.

You mentioned that you tried to setInterval and it didn't work, so please note that I commented alert() in the code above because it caused me to have delays, without it the time stamp in html updates every 3000ms with an accuracy about 5ms.

如果您要使用watchPosition(),则上述方法有效,因为clearWatch()停止watchPosition()并进一步回调.基本上,以上代码每三秒钟启动一次新的watchPosition(),并使用clearWatch()将其杀死.就是说,我认为watchPosition()并不是在此方法中使用的最佳方法,除非您期望在此时间间隔内位置发生重大变化.我宁愿使用getCurrentPosition(),因为它可以完成工作并且不等待位置变化.

The above approach works if you want to use watchPosition() because clearWatch() stoppes the watchPosition() and it further callbacks. Basically the code above initiates new watchPosition() every three seconds and kills it with clearWatch(). That is being said I think watchPosition() is not the best method to use in this approach unless you expect significant changes in position within this time interval. I would rather use getCurrentPosition() since it does the job and doesn't wait for changes in position.

 var watchPositionId;
   //start watching location
    setInterval(function () {
        watchPositionId = navigator.geolocation.getCurrentPosition(updateCompass, handleerror, {
        enableHighAccuracy: true,
        maximumAge: 3000,
        timeout: 3000
    });
    }, 3000);

    function updateCompass(p) {
        mytimestamp.innerHTML = p.timestamp;
    }

这篇关于地理位置监视无法持续启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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