【发布时间】:2018-11-09 04:54:52
【问题描述】:
我正在尝试创建一个非常简单的应用程序,它将获取设备的当前位置并每隔 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
});
【问题讨论】:
标签: android ios cordova cordova-plugins phonegap