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