Skip to content

Instantly share code, notes, and snippets.

@christocracy
Last active September 6, 2018 15:31

Revisions

  1. christocracy revised this gist Jan 27, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion BackgroundGeolocation-Ionic2.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /**
    * How to implement cordova-background-geolocation with Ionic 2
    * How to implement cordova-background-geolocation with Ionic 2 / 3
    * https://github.com/transistorsoft/cordova-background-geolocation-lt
    * Chris Scott, Transistor Software <chris@transistorsoft.com>
    */
  2. christocracy revised this gist Jan 27, 2018. No changes.
  3. christocracy revised this gist Feb 9, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion BackgroundGeolocation-Ionic2.js
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ export class HomePage {
    this.bgGeo.on('motionchange', this.onMotionChange.bind(this));
    this.bgGeo.on('activitychange', this.onActivityChange.bind(this));
    this.bgGeo.on('geofence', this.onGeofence.bind(this));
    this.bgGeo.on('http', this.onHttpSuccess, this.onHttpFailure.bind(this));
    this.bgGeo.on('http', this.onHttpSuccess.bind(this), this.onHttpFailure.bind(this));

    // 3. Configure it.
    this.bgGeo.configure({
  4. christocracy created this gist Feb 9, 2017.
    69 changes: 69 additions & 0 deletions BackgroundGeolocation-Ionic2.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    /**
    * How to implement cordova-background-geolocation with Ionic 2
    * https://github.com/transistorsoft/cordova-background-geolocation-lt
    * Chris Scott, Transistor Software <chris@transistorsoft.com>
    */

    import { Component } from '@angular/core';

    import { NavController, Platform } from 'ionic-angular';

    @Component({
    selector: 'page-home',
    templateUrl: 'home.html'
    })
    export class HomePage {
    /**
    * Reference to BackgroundGeolocation
    */
    private bgGeo: any;

    constructor(public navCtrl: NavController, public platform: Platform) {
    platform.ready().then(this.configureBackgroundGeolocation.bind(this));
    }
    configureBackgroundGeolocation() {
    // 1. Get a reference to the plugin
    this.bgGeo = (<any>window).BackgroundGeolocation;

    // 2. Listen to events
    this.bgGeo.on('location', this.onLocation.bind(this));
    this.bgGeo.on('motionchange', this.onMotionChange.bind(this));
    this.bgGeo.on('activitychange', this.onActivityChange.bind(this));
    this.bgGeo.on('geofence', this.onGeofence.bind(this));
    this.bgGeo.on('http', this.onHttpSuccess, this.onHttpFailure.bind(this));

    // 3. Configure it.
    this.bgGeo.configure({
    debug: true,
    desiredAccuracy: 0,
    distanceFilter: 10,
    url: 'http://192.168.11.100:8080/locations',
    autoSync: true
    }, (state) => {
    // 4. Start the plugin.
    this.bgGeo.start();
    });
    }

    onLocation(location, taskId) {
    console.log('- location: ', location);
    this.bgGeo.finish(taskId);
    }
    onMotionChange(isMoving, location, taskId) {
    console.log('- motionchange: ', isMoving, location);
    this.bgGeo.finish(taskId);
    }
    onActivityChange(activity) {
    console.log('- activitychange: ', activity);
    }
    onGeofence(params, taskId) {
    console.log('- geofence: ', params);
    this.bgGeo.finish(taskId);
    }
    onHttpSuccess(response) {
    console.log('- http success: ', response);
    }
    onHttpFailure(response) {
    console.log('- http failure: ', response);
    }
    }