简体   繁体   中英

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. 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.

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. 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).

I even tried to make it work with navigator.geolocation.watchPosition, but same issue. 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.

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
    });

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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