【问题标题】:Geo-Location Custom Update Interval (30 seconds)地理位置自定义更新间隔(30 秒)
【发布时间】:2014-12-13 15:08:28
【问题描述】:

我有一个地理位置检查移动应用程序(用于路线跟踪),我需要能够为用户可以修改的位置更新指定自定义时间间隔。我正在使用 watchPosition() 函数,因为准确性很重要,而 getCurrentPosition() 似乎不能很好地工作。但是,我仍然希望用户能够根据自己的喜好自定义更新间隔。

到目前为止,这是我的代码的粗略表示:http://jsfiddle.net/ppyordanov/taz4g5t0/5/

html:

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

JS:

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

};

setGeolocation();

window.setTimeout( function () {
        setGeolocation();
    }, 
    30000 //check every 30 seconds
);

我可以使用 setTimeout() 来操纵时间间隔,但它似乎对输出没有任何影响。它大约每秒更新一次。如何将其增加到 30 秒?

【问题讨论】:

    标签: javascript html geolocation navigator


    【解决方案1】:

    我不是每隔n 秒调用一次setGeolocation();,而是将位置保存在window.navigator.geolocation.watchPosition() 调用中,然后通过setInterval() 函数在单独的函数中更新它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多