Skip to content

Instantly share code, notes, and snippets.

@christocracy
Last active September 2, 2020 12:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christocracy/0379dd519d77f4215d5de943ed51ade9 to your computer and use it in GitHub Desktop.
Save christocracy/0379dd519d77f4215d5de943ed51ade9 to your computer and use it in GitHub Desktop.
/**
* How to implement cordova-background-geolocation with Ionic 2 / 3
* @see https://github.com/transistorsoft/cordova-background-geolocation-lt
* @author Chris Scott, Transistor Software <chris@transistorsoft.com>
*/
import { Component } from '@angular/core';
import { NavController, Platform } from 'ionic-angular';
// Import the SDK in addition to any desired interfaces:
import BackgroundGeolocation, {
Location,
MotionChangeEvent,
MotionActivityEvent,
GeofenceEvent,
Geofence,
HttpEvent,
ConnectivityChangeEvent
} from "cordova-background-geolocation-lt"; // <-- or "cordova-background-geolocation" for licensed customers
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController, public platform: Platform) {
platform.ready().then(this.configureBackgroundGeolocation.bind(this));
}
configureBackgroundGeolocation() {
// 1. Listen to events (see the docs a list of all available events)
BackgroundGeolocation.onLocation(this.onLocation.bind(this));
BackgroundGeolocation.onMotionChange(this.onMotionChange.bind(this));
BackgroundGeolocation.onActivityChange(this.onActivityChange.bind(this));
BackgroundGeolocation.onGeofence(this.onGeofence.bind(this));
BackgroundGeolocation.onHttp(this.onHttp.bind(this));
BackgroundGeolocation.onEnabledChange(this.onEnabledChange.bind(this));
BackgroundGeolocation.onConnectivityChange(this.onConnectivityChange.bind(this));
// 2. Configure the plugin
BackgroundGeolocation.ready({
debug: true,
logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
distanceFilter: 10,
stopOnTerminate: false,
startOnBoot: true,
url: 'http://your.server.com/locations',
autoSync: true,
params: {
foo: 'bar'
}
}, (state) => {
// Note: the SDK persists its own state -- it will auto-start itself after being terminated
// in the enabled-state when configured with stopOnTerminate: false.
// - The #onEnabledChange event has fired.
// - The #onConnectivityChange event has fired.
// - The #onProviderChange has fired (so you can learn the current state of location-services).
if (!state.enabled) {
// 3. Start the plugin. In practice, you won't actually be starting the plugin in the #ready callback
// like this. More likely, you'll respond to some app or UI which event triggers tracking. "Starting an order"
// or "beginning a workout", for example.
BackgroundGeolocation.start();
} else {
// If configured with stopOnTerminate: false, the plugin has already begun tracking now.
// - The #onMotionChange location has been requested. It will be arriving some time in the near future.
}
});
}
onLocation(location:Location) {
console.log('[location] -', location);
}
onMotionChange(event:MotionChangeEvent) {
console.log('[motionchange] -', event.isMoving, event.location);
}
onActivityChange(event:MotionActivityEvent) {
console.log('[activitychange] -', event.activity, event.confidence);
}
onGeofence(event:GeofenceEvent) {
console.log('[geofence] -', event.action, event.identifier, event.location);
}
onHttp(event:HttpEvent) {
console.log('[http] -', event.success, event.status, event.responseText);
}
onEnabledChange(enabled:boolean) {
console.log('[enabledchange] - enabled? ', enabled);
}
onConnectivityChange(event:ConnectivityChangeEvent) {
console.log('[connectivitychange] - connected?', event.connected);
}
}
@cutechsupport
Copy link

Hi,
So i need to put npm install for both dependencies

  1. npm install ionic-angular
  2. npm install cordova-background-geolocation-lt
    Is it correct.
    Thanks.

@Taimoors95
Copy link

Hi, I want to add geofence on a location. I have added geofence in initState by specifying lat lng. Now what I would have to do check whether current user has entered in a geofence or not

Thanks

@sandrojr
Copy link

sandrojr commented Sep 2, 2020

Hi, I'd tried this code but happen a specific error:
"ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'apply' of undefined
TypeError: Cannot read property 'apply' of undefined"

code affected:

"BackgroundGeolocation.onLocation = function (success, failure) {

    var bgGeo = plugin();

    bgGeo.onLocation.apply(bgGeo, arguments);

};

Thanks.

@christocracy
Copy link
Author

If you have a problem, post an issue at Github repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment