abhilash reddy
abhilash reddy

Reputation: 1586

Location tracking in background and foreground

I am building an Android app for a company, where the users will work in remote locations. So the owner of the company needs to track the location of the users. The Company employees will login into the app and we need to begin the tracking. I want the location of the user every 30 minutes(even if the user has/hasn't moved from the previous location).

I just want to know, can I achieve this functionality using capacitor geolocation and setInterval in my Ionic 4 project?

Upvotes: 1

Views: 3774

Answers (2)

Stefan
Stefan

Reputation: 111

For those who are using capacitor with Ionic follow the instructions here and install packages with:

npm install @mauron85/cordova-plugin-background-geolocation
npm install @ionic-native/background-geolocation
ionic cap sync

When working with Android, you are most likely get build errors. Here are the steps that helped us to get the plugin running:

  • Make sure to migrate to AndroidX (in Android Studio select "Refactor/Migrate to AndroidX...")
  • Add the following lines to resources section in your strings.xml file which is located in the Android folder that capacitor creates:

<string name="mauron85_bgloc_account_name">@string/app_name</string>
<string name="mauron85_bgloc_account_type">$PACKAGE_NAME.account</string>
<string name="mauron85_bgloc_content_authority">$PACKAGE_NAME</string>

Upvotes: 2

Najam Us Saqib
Najam Us Saqib

Reputation: 3402

Didn't Use Capacitor yet. But Yes you can achieve Background Geolocation Plugin for this.

Minimal Example:

import { BackgroundGeolocation, BackgroundGeolocationConfig, BackgroundGeolocationResponse } 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)
  .then(() => {

    this.backgroundGeolocation.on('location').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 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.
this.backgroundGeolocation.stop();

https://ionicframework.com/docs/native/background-geolocation

Upvotes: 0

Related Questions