[英]Location tracking in background with PhoneGap
我正在嘗試創建一個非常簡單的應用程序,它將獲取設備的當前位置,並每隔15分鍾左右將其發布到服務器上。 我正在使用cordova-plugin-background-fetch( https://github.com/transistorsoft/cordova-plugin-background-fetch ),它可以在大約15分鍾(約等)喚醒應用程序,但問題仍然存在我面臨的是獲取GPS坐標。
我正在嘗試使用navigator.geolocation.getCurrentPosition來獲取它,在啟動BackgroundFetch之前,我使用navigator.geolocation.getCurrentPosition進行了一次測試調用,以檢查它是否有效並具有所有權限。 它與示例回調函數中的代碼相同,並且在第一個測試調用上運行良好。
我面臨的問題是,一旦BackgroundFetch喚醒應用程序,則在回調函數navigator.geolocation.getCurrentPosition中始終會失敗(錯誤代碼3-超時已過期)。
我什至試圖使它與navigator.geolocation.watchPosition一起使用,但存在相同的問題。 第一次啟動時,它可以工作。 一旦回調在后台啟動,它將失敗(再次超時)。
我不想一直觀察位置並耗盡電池,我真的每15-30分鍾只需要用一次。
這是我正在使用的代碼,歡迎大家提供幫助和建議!
謝謝!
BackgroundFetch.configure(function() {
console.log('[js] BackgroundFetch event received');
navigator.geolocation.getCurrentPosition(function(position){
console.log('we have location now');
$.post('https://www.example.com/api/location/', {
Lat: position.coords.latitude,
Lon: position.coords.longitude
}, function(data){
window.BackgroundFetch.finish();
}, 'json').fail(function(){
window.BackgroundFetch.finish();
});
}, function(error){
console.log('Error code: ' + error.code);
console.log('Error message: ' + error.message);
window.BackgroundFetch.finish();
}, {
enableHighAccuracy: false,
timeout: 20000,
maximumAge: 0
});
}, function(error) {
console.log('- BackgroundFetch failed', error);
}, {
minimumFetchInterval: 15
});
我想知道的問題是,用戶必須實際接受一項地理位置服務才能獲取
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.