【发布时间】: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