2013-04-24 77 views
0

我正在爲一家物流公司編寫一個跟蹤應用程序,該應用程序需要定期將每部手機的位置報告回基地。 geolocation.watchPosition方法似乎是理想的,只是據我可以告訴它更新位置經常每秒一次,似乎沒有辦法增加回調之間的時間間隔。從數據使用的角度來看,每秒一次的次數太多 - 每分鐘一次會是理想的。降低地理位置的頻率watchPosition調用

是否有一個選項可以傳遞給watchPosition使其以最小時間間隔執行回調?

回答

1

您可以允許它每秒鐘仍然調用一次watchPosition,但是然後將報告返回分離出一個單獨的回調,該回調在計時器上每分鐘運行一次。

例如爲:

var GPSStore = {}; 
navigator.geolocation.watchPosition(
    function(current_location) { 
    // Just store the latest co-ordinates in memory as often as they are generated 
    GPSStore.location = current_location; 
    } 
); 
// Once a minute transmit the latest co-ordinates 
setInterval(function() { 
    transmitLocation(GPSStore.location); 
}, 1000*60);