簡體   English   中英

地理位置監視無法持續啟動

[英]Geolocation watch doesn't fire consistently

我有一個測試應用程序,可從HTML5地理位置服務中獲取經/緯度值。 不幸的是,它以非常不一致的時間間隔觸發,從500毫秒到10000毫秒之間不等。 我嘗試更改手表的maximimAge和timeout參數,但這些參數似乎沒有任何變化。 我正在Chrome中以及通過Android Lollipop構建中的簡單Cordova應用進行測試。 下面的代碼僅顯示手表的時間戳值,以消除可能引起此問題的任何其他延遲。 似乎該間隔接近1秒然后5秒的重復模式。 我還嘗試過將地理位置提取功能放置在setInterval函數中,並且其行為具有相同的1和5秒重復間隔。

<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 ……實現可能會限制回調的頻率,以避免無意中消耗過多的回調。資源“。

這意味着watchPosition()正在等待將導致位置變化發生的事件,否則將不執行任何操作。 因此,除非看到位置變化,否則watchPosition()將不會調用updateCompass()。 另外,關於watchPosition(), maximumAgetimeout參數與獲取新位置有關,這些參數與watchPosition()調用的次數和時間無關。

WatchPosition()防止連續調用以查看位置是否發生變化,並且僅在發生某些與位置變化有關的事件時才作出反應,這有助於節省電池等資源。 但是,如果您真的想查看位置,則假設每三秒鍾一次,無論位置是否更改,我都認為設置間隔是個好方法。

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中的時間戳將每3000ms更新一次,精度約為5ms 。

如果您想使用watchPosition(),則上述方法有效,因為clearWatch()阻止了watchPosition()並進一步回調了該方法。 基本上,以上代碼每三秒鍾啟動一次新的watchPosition(),並使用clearWatch()將其殺死。 就是說,我認為watchPosition()並不是在此方法中使用的最佳方法,除非您期望在此時間間隔內位置發生重大變化。 我寧願使用getCurrentPosition(),因為它可以完成工作並且不等待位置變化。

 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;
    }

我最終更改了解決方案,以同時使用地理位置和deviceOrientationEvent。 單獨使用地理位置事件會產生非常“生澀的”響應,而設備定位事件會消除這種情況。

這是setInterval函數,設置為每十秒鍾獲取一次地理位置:

window.onload = function()
{
    //fetch new GPS info every 10000 milliseconds
    window.setInterval(function ()
    {
        navigator.geolocation.getCurrentPosition(onGeoSuccess, onGeoFailure, {
        enableHighAccuracy: true,
        maximumAge: 0,
        timeout: 30000 //large timeout to accomodate slow GPS lock on some devices
    });
    }, 10000);  //update current location every ten seconds
};

關於地理上的成功:

function onGeoSuccess(location)
{

    //call function to calculate bearing
    var cluebearing = geo.bearing(currentlat, currentlng, cluelat, cluelng);

    //call function to rotate arrow
    rotateArrow(cluebearing);
}//end onGeoSuccess()

獲取設備方向和過程的功能:

function rotateArrow(mybearing)
{
    //Check for support for DeviceOrientation event
    if(window.DeviceOrientationEvent)
    { 
        //orientation listener
        window.addEventListener('deviceorientation', function(event)
        {
            var myalpha = event.alpha; //rotation from north
            //process orientation change
        }//end deviceorientation listener
    }
}//end rotateArrow()

關於地理故障:

function onGeoFailure()
{
    alert('Please turn on your phone\'s GPS');
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM