userlkjsflkdsvm
userlkjsflkdsvm

Reputation: 973

background geolocation information to send to server ionic 2+

I would like to send different config settings from the background geolocation plugin to my server. However, I am not sure how to get that to work. I have the location server ready for the most part. Just not sure how to send that data. I would like to place the start tracking config in a post request. But not sure how to retrieve that info and if the code I have below is even correct. Thanks!

location.ts

import { Injectable, NgZone } from '@angular/core';
import { BackgroundGeolocation, BackgroundGeolocationConfig } from '@ionic-native/background-geolocation';
import { Geolocation, Geoposition } from '@ionic-native/geolocation';
import 'rxjs/add/operator/filter';

@Injectable()
export class LocationTracker {

  public watch: any;    
  public lat: number = 0;
  public lng: number = 0;
  public bearing: number = 0;
  public speed: number = 0;

  constructor(public zone: NgZone, public backgroundGeolocation: BackgroundGeolocation, public geolocation: Geolocation) {

  }

  startTracking() {

    let config = {
    desiredAccuracy: 0,
    stationaryRadius: 20,
    distanceFilter: 10, 
    debug: true,
    interval: 2000,
    bearing: 10,
    speed: 0,

  };

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

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

  }, (err) => {

    console.log(err);

  });

  // Turn ON the background-geolocation system.
  this.backgroundGeolocation.start();


  // Foreground Tracking

let options = {
  frequency: 60000, 
  enableHighAccuracy: true
};

this.watch = this.geolocation.watchPosition(options).filter((p: any) => p.code === undefined).subscribe((position: Geoposition) => {

  console.log(position);

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

});

  }

  stopTracking() {
        console.log('stopTracking');
        this.backgroundGeolocation.finish();
        this.watch.unsubscribe();
  }

}

Upvotes: 1

Views: 972

Answers (1)

Apit John Ismail
Apit John Ismail

Reputation: 2145

Your code is for Ionic 4. Not sure if it is applicable to ionic 2

Here is what I did.

The instruction at https://ionicframework.com/docs/native/background-geolocation is very good. Only missing small part.

The missing code is what I considered important in order to keep on sending the location lat and long. I highlight the missing code.

import { BackgroundGeolocation, BackgroundGeolocationConfig, BackgroundGeolocationEvents, BackgroundGeolocationResponse } from '@ionic-native/background-geolocation';

constructor(private backgroundGeolocation: BackgroundGeolocation) { }

...
// Wrap this in a function to activate

const config: BackgroundGeolocationConfig = {
            desiredAccuracy: 10,
            stationaryRadius: 2, //I reduce this from 20 to 2
            distanceFilter: 3, //I reduce this from 30 to 3
            interval: 2000, //This is missing from example
            debug: true, //  enable this hear sounds for background-geolocation life-cycle.
            stopOnTerminate: false, // enable this to clear background location settings when the app terminates
    };

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

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

  console.log(location);

  //DO YOUR HTTP POST HERE TO SEND LOCATION LAT AND LONG TO SERVER

      // 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 operations are successful or not.
      // IF YOU DON'T, ios will CRASH YOUR APP for spending too much time in the background.
      this.backgroundGeolocation.finish(); // FOR IOS ONLY
    });

  });

// start recording location
this.backgroundGeolocation.start();

// If you wish to turn OFF background-tracking, call the #stop method.
// Wrap this inside a call off function to stop backgroundGeolocation
this.backgroundGeolocation.stop();

Upvotes: 1

Related Questions