background geolocation ionic 3 not updating

Viewed 6378

Background geolocation plugin for ionic isn't updating. The functionality I want is every 30 seconds ask the plugin for a lat lng value if available. The issue is, it's only giving me the values initially and then the background stops. The foreground is fine, it's really the background. Basically I'm not able to send the requests after the first initial send in the background.

gps.ts

startTracking() {
  console.log("started tracking");
  const config: BackgroundGeolocationConfig = {
    desiredAccuracy: 10,
    stationaryRadius: 20,
    distanceFilter: 30,
    debug: false, //  enable this hear sounds for background-geolocation life-cycle.
    stopOnTerminate: false
  };

  this.backgroundGeolocation
    .configure(config)
    .subscribe((location: BackgroundGeolocationResponse) => {
      this.zone.run(() => {
        this.lat = location.latitude;
        this.lng = location.longitude;
        this.bearing = location.bearing;
        this.speed = location.speed;
        this.accuracy = location.accuracy;
        this.timestamp = location.time;
      });

      this.backgroundGeolocation.finish(); // FOR IOS ONLY
      this.backgroundGeolocation.stop();
    });

  this.backgroundGeolocation.start();
}

sendGPS() {
  this.optionsService.sendGPS(gpsData).subscribe(result => {});
}

stopTracking() {
  this.sendGPS();
}

app.component.ts

constructor() {
  this.sendGPSStart();
  this.interval();
}

sendGPSStart() {
  this.locationTracker.startTracking();
}

sendGPSStop() {
  this.locationTracker.stopTracking();
}

interval() {
  setInterval(() => {
    this.sendGPSStart();
    this.sendGPSStop();
  }, "30000");
}
5 Answers

Looking at examples e.g dnchia/Ionic3-Background-Geolocation you would configure the interval on the background, as well as the periodic foreground send

gps.ts

startTracking(interval) {

    console.log('started tracking')
    const config: BackgroundGeolocationConfig = {
      desiredAccuracy: 10,
      stationaryRadius: 20,
      distanceFilter: 30,
      debug: false, //  enable this hear sounds for background-geolocation life-cycle.
      stopOnTerminate: false,
      interval: interval
    };

app.component.ts

interval = 30000;

constructor() {
  this.sendGPSStart()
  this.interval()
}

sendGPSStart(){
  this.locationTracker.startTracking(this.interval);
}

sendGPSStop(){
  this.locationTracker.stopTracking();
}

interval() {
  setInterval(() => {
    this.locationTracker.sendGPS();
  }, this.interval)

}

In your config, you must determine the interval in miliseconds.

const config: BackgroundGeolocationConfig = {
  desiredAccuracy: 10,
  stationaryRadius: 20,
  distanceFilter: 30,
  debug: false, //  enable this hear sounds for background-geolocation life-cycle.
  stopOnTerminate: false,
  interval: 2000 // <= example 2 seconds interval
};

Then in the subscribe section do a ajax call like

    this.backgroundGeolocation.on(BackgroundGeolocationEvents.location).subscribe((location: BackgroundGeolocationResponse) => {
          console.log(location);

          this.postData = {
            user_id: this.userId,
            latitude: location.latitude,
            longitude: location.longitude,
          };

          console.log(location);

          this.http.post<ActivationResponse>(environment.crmUrl + '/api/start-job', this.postData,
            { headers: new HttpHeaders({ Authorization: this.token }), observe: 'response' })
            .subscribe(async (response: HttpResponse<ActivationResponse>) => {

              console.log(response);
              this.backgroundGeolocation.finish(); // FOR IOS ONLY

            }, async (errorResponse: HttpErrorResponse) => {

              console.log(errorResponse);

            });
});

It will send new gps location to your determined server every 2 seconds automatically.

All locations (even a single one) will be sent as an array of object(s), when postTemplate is jsonObject and array of array(s) for jsonArray!

In server-side, I changed the JSON object to an array of object.

For example,

{
  "user_id": "1",
  "lon": "13.2",
  "lat": "82.3"
}

I changed the above JSON object to following

[{
  "user_id": "1",
  "lon": "13.2",
  "lat": "82.3"
}]
Related