2014-12-13 69 views
0

我有一個地理位置檢查移動應用程序(用於路線跟蹤),我需要能夠指定用戶可以修改的位置更新的自定義時間間隔。我正在使用watchPosition()函數,因爲準確性很重要,而getCurrentPosition()似乎不能很好地工作。但是,我仍然希望用戶能夠根據自己的喜好自定義更新間隔。地理位置自定義更新間隔(30秒)

以下是我的代碼的粗略表示: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秒?

回答

1

與其說setGeolocation();n秒的,我保存在window.navigator.geolocation.watchPosition()通話的位置,然後在通過setInterval()功能的單獨功能更新。

相關問題