我正在尝试创建一个非常简单的应用程序,它将获取设备的当前位置,并每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
});
我解决的问题是,用户必须亲自接受一个地理位置服务,以便获取它们