Ionic Background Geolocation


Ionic Background Geolocation Native Plugin– Background Geolocation plugin is used basically to get the foreground and background geolocation. It is very simple to install the background geolocation plugin to access the native geolocation feature. Here in this tutorial, we are going to explain how you can install and use this plugin.


Ionic Background Geolocation Native | Cordova Plugin Example

First you need to install the cordova plugin cordova-plugin-mauron85-background-geolocation.

Installation

You can install the plugin simply as below-

Ionic Background Geolocation Cordova Plugin:

$ ionic cordova plugin add cordova-plugin-mauron85-background-geolocation
$ npm install --save @ionic-native/background-geolocation

Platform

  • IOS
  • Android

This plugin is supported in both IOS and Android Platforms.

Example

| Example:

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

constructor(private backgroundGeolocation: BackgroundGeolocation) { }

...

const config: BackgroundGeolocationConfig = {
            desiredAccuracy: 10,
            stationaryRadius: 20,
            distanceFilter: 30,
            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)
  .subscribe((location: BackgroundGeolocationResponse) => {

    console.log(location);

    // 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 HTTP request is 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.
this.backgroundGeolocation.stop();

Instance Members

  • Location Provider – This is used to set the location service provider.
  • Accuracy – Desired Accuracy in Meters[0, 10, 100, 1000], the lower number means higher accuracy reading, higher number means lower accuracy.
  • Mode – Used in switch mode function values possible are – BACKGROUND:0, FOREGROUND:1.
  • configure(options)
  • start() – Start geolocation.
  • stop() – Stop geolocation.
  • finish() Inform Native plugin that you are done.
  • changePace(isMoving)– Force Plugin to enter in moving or stationary state.
  • setConfig(options) – This is used to setup the configuration.
  • getStationaryLocation() – This returns the current stationary location.
  • onStationary() – A listener to stationary state.
  • isLocationEnabled() Checks whether the location is enabled or not on your device.
  • showAppSettings() – Show settings to change permissions.
  • showLocationSettings()
    – This shows the device location settings.
  • watchLocationMode()– This basically monitors if user changes the location settings in device.
  • stopWatchingLocationMode()– Stop Watching the location mode.
  • getLocations()– This returns the stored locations.
  • getValidLocations()-Get the valid locations.
  • deleteLocation(locationId)– Delete stored location by location Id.
  • deleteAllLocations()– Delete all stored locations.
  • switchMode(modeId)– Switch mode between background and foreground.

Advertisements

Add Comment

📖 Read More