繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM