简体   繁体   English

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

[英]Location tracking in background with PhoneGap

I'm trying to create very simple application that will get the current location of the device and post it to server every 15 minutes or so. 我正在尝试创建一个非常简单的应用程序,它将获取设备的当前位置,并每隔15分钟左右将其发布到服务器上。 I'm using cordova-plugin-background-fetch ( https://github.com/transistorsoft/cordova-plugin-background-fetch ) and it works just fine awaking the app on about 15 minutes (or so), but the issue I'm facing is to get GPS coordinates. 我正在使用cordova-plugin-background-fetch( https://github.com/transistorsoft/cordova-plugin-background-fetch ),它可以在大约15分钟(约等)唤醒应用程序,但问题仍然存在我面临的是获取GPS坐标。

I'm trying to get it using navigator.geolocation.getCurrentPosition and before I start BackgroundFetch, I make one test call with navigator.geolocation.getCurrentPosition to check if it works and have all the permissions. 我正在尝试使用navigator.geolocation.getCurrentPosition来获取它,在启动BackgroundFetch之前,我使用navigator.geolocation.getCurrentPosition进行了一次测试调用,以检查它是否有效并具有所有权限。 It is the same code as in example callback function and it works just fine on that first test call. 它与示例回调函数中的代码相同,并且在第一个测试调用上运行良好。

The problem I'm facing is that once BackgroundFetch awakes the application, in callback function navigator.geolocation.getCurrentPosition always fail (error code 3 - timeout expired). 我面临的问题是,一旦BackgroundFetch唤醒应用程序,则在回调函数navigator.geolocation.getCurrentPosition中始终会失败(错误代码3-超时已过期)。

I even tried to make it work with navigator.geolocation.watchPosition, but same issue. 我什至试图使它与navigator.geolocation.watchPosition一起使用,但存在相同的问题。 The first time I start it, it works. 第一次启动时,它可以工作。 Once callback start it in background, it fails (again timeout expired). 一旦回调在后台启动,它将失败(再次超时)。

I don't want to watch position all the time and drain the battery, I really need it only once in every 15-30 minutes. 我不想一直观察位置并耗尽电池,我真的每15-30分钟只需要用一次。

Here is the code that I'm using and every help and suggestion is welcome! 这是我正在使用的代码,欢迎大家提供帮助和建议!

Thank you! 谢谢!

    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