Background Geolocation server interaction


#1

Hello All,

I’m using cordova-plugin-mauron85-background-geolocation for fetching the location in the background even when the app is not running in the foreground. What I want to achieve is to get the coordinates of the user and send it or post it to the server for real-time location tracking and updating the DB.
I think the problem is location points are updating in the background but the data is not posted to the server.
Is there any way to interact with the server when the app is not in the foreground.
This is my code:

// Background Tracking
let config: BackgroundGeolocationConfig = {
desiredAccuracy: 0,
stationaryRadius: 20,
distanceFilter: 10,
debug: true,
interval: 2000
};

this.backgroundGeolocation
.configure(config)
.subscribe((location: BackgroundGeolocationResponse) => {

  console.log('BackgroundGeolocation:  ' + location.latitude + ',' + location.longitude);

  // Run update inside of Angular's zone
  this.zone.run(() => {
    this.lat = location.latitude;
    this.lng = location.longitude;
  });

  // Server interaction to POST location coordinates.

}, (err) => {

  console.log("ERROR in config", err);

});

Any help will be highly appreciated.
Thanks


#2

@charlestsmith, did you ever get this working. Struggling with this myself right now. Thx,


#3

@charlestsmith @evandillon This is working for me

let config: BackgroundGeolocationConfig = {
    desiredAccuracy: 10,
    stationaryRadius: 2,
    distanceFilter: 2,
    debug: true,
    interval: 2000,
    stopOnTerminate: false,
    url: "put-the-url-of-file-here",
    postTemplate: {
        lat: '@latitude',
        lng: '@longitude'
    }
};

this.backgroundGeolocation.configure(config).then((location: BackgroundGeolocationResponse)=>{
    ...
});

Location is sent to server every time it changes when app is in background or foreground.