Last active
September 11, 2024 12:19
-
-
Save christocracy/0379dd519d77f4215d5de943ed51ade9 to your computer and use it in GitHub Desktop.
BackgroundGeolocation with Ionic 2+ (https://www.transistorsoft.com/shop/products/cordova-background-geolocation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* How to implement cordova-background-geolocation with Ionic 2 / 3 | |
* @see https://github.com/transistorsoft/cordova-background-geolocation-lt | |
* @author Chris Scott, Transistor Software <[email protected]> | |
*/ | |
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment