Search code examples
ionic2geolocationionic3cordova-plugins

Updating location data into the server is not working using background geo locaion in ionic 3


I am using https://github.com/mauron85/cordova-plugin-background-geolocation. It gives location details in the foreground and gives debug messages when the app is closed but it does not update the location details into the server both foreground and background.

Thanks in advance

const config: BackgroundGeolocationConfig = {
    desiredAccuracy: 10,
    stationaryRadius: 10,
    distanceFilter: 10,
    debug: false,
    stopOnTerminate: false,
    startForeground: true,
    notificationTitle: 'location tracking',
    notificationText: 'Active',
    interval: 60000,

    url: localStorage.getItem('api_base_url')+'user/currentlocation',
    syncUrl:localStorage.getItem('api_base_url')+'user/currentlocation',
    httpHeaders: {
      'Content-Type': 'application/json'
    },
    postTemplate: {
      lat: '@latitude',
      lon: '@longitude',
      user_id: '1',
      currentDate: '12-12-2019',
      address: 'test',
    }
    };

this.backgroundGeolocation.configure(config)
  .then(() => {

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

  });

this.backgroundGeolocation.start();

Solution

  • Background geo location post template

    Keep in mind that 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"
    }]