使用PhoneGap在后台进行位置跟踪

时间:2018-11-09 04:54:53

标签: android ios cordova cordova-plugins phonegap

我正在尝试创建一个非常简单的应用程序,它将获取设备的当前位置,并每隔15分钟左右将其发布到服务器上。 我正在使用cordova-plugin-background-fetch(https://github.com/transistorsoft/cordova-plugin-background-fetch),它在大约15分钟(或大约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
    });

1 个答案:

答案 0 :(得分:-1)

我想知道的问题是,用户必须实际接受一项地理位置服务才能获取它们