How to use BackgroundGeolocation?


#1

Hi,

I am using the BackroundGeolocation plugin but I don’t get a succes callback every time there is an new position. I made an interval to lookup for new locations but I thought that I can get the new location in BackgroundGeolocation.configure() method. Does somebody know how to use this plugin the right way?

Here is my code:

constructor(public platform: Platform) {

    this.locations = [];

    platform.ready().then(() => {
        let config: any = {
            interval: 2,                
            locationTimeout: 2,
            desiredAccuracy: 10,
            stationaryRadius: 20,
            distanceFilter: 2,
            debug: true, //  enable this hear sounds for background-geolocation life-cycle.
            stopOnTerminate: false, // enable this to clear background location settings when the app terminates
        };
        BackgroundGeolocation.configure(config).then((location) => {
            this.startTrackingInterval();
            console.log('[js] BackgroundGeolocation callback:  ' + location.latitude + ',' + location.longitude);
            // IMPORTANT:  You must execute the finish method here to inform the native plugin that you're finished,
            // and the background-task may be completed.  You must do this regardless if your HTTP request is successful or not.
            // IF YOU DON'T, ios will CRASH YOUR APP for spending too much time in the background.
            // BackgroundGeolocation.finish(); // FOR IOS ONLY
        })
            .catch((error) => {
                console.log('BackgroundGeolocation error');
            });
    });
}
addLocation(locations: any): void {
    this.locations = locations;
}
startTrackingInterval(): void {
    this.trackerInterval = setInterval(() => { this.refreshLocations(); }, 5000);
}
refreshLocations(): void {
    BackgroundGeolocation.getLocations().then(locations => {
        this.locations = locations;
    }).catch(error => {
        console.log(error);
    });
}
startTracker(): void {
    BackgroundGeolocation.deleteAllLocations();
    BackgroundGeolocation.start();
}

stopTracking(): void {
    clearInterval(this.trackerInterval);
    BackgroundGeolocation.getLocations().then(locations => {
        this.locations = locations;
    }).catch(error => {
        console.log(error);
    });
    BackgroundGeolocation.stop();
}

#2

Hi Pascal,

I’ve the same problem with my code.

Martijn


#3

In my case, I’ve followed this: http://www.joshmorony.com/adding-background-geolocation-to-an-ionic-2-application/

But everytime I set the app in background, It’s like the background location it closes…


#4

Wish I had more to add then +1, but that is what I have. We are on beta 7. I can see the plugin running, but the callback does not get updated.


#5

Yes, and why? I don’t know why, sometimes looks like is working…