Ionic 3 | Get Background Device Location using Cordova and Ionic Native Geolocation Plugins

Applications these days provide features to get locations like we implemented device location plugin to get location coordinates when a user clicks a button, But what if we want to get location updates after fixed intervals of time. To track location we can’t always keep an application open, In that case, we need to track location when the application is in the background.

In this tutorial, we will create an Ionic 3 application to track Background Geolocation using Cordova plugin and Ionic Native wrapper.

So, let’s begin …

Before we start with application make sure that your system is having updated version of NodeJS installed.

Create a new Ionic Application

Install latest version of Ionic and Cordova CLI.

Now we will create a new blank application

After the application is created, go to the root using the CD command

If your system is having Visual Studio code(VS Code) installed, use  $ code .  command to open the project in VS Code

Install the Cordova and Ionic Native plugins

Background Geolocation

This plugin will use device location service to fetch latitude, longitude, accuracy, altitude, altitudeAccuracy, altitudeAccuracy, heading, speed and start a background service to update location after an interval.

Local Notifications

We are using Local Notification plugin to show location coordinates received from Background Geolocation plugin

Add these plugin in application’s module file

In app.module.ts, import plugin and add in imports array.

In home.html, add following HTML

Here we have two buttons to start and stop background geolocation service.

Update component with methods

Above code is having Background Geolocation configuration object, and methods to start and stop Background location service. On successfully fetching location object we will show Local Notification having data of coordinates, accuracy, speed, time etc.

That’s it you can now test your application in a real device after building apk using this command $ ionic cordova build android 

Let me know if you face any issue in implementing this

Happy coding 🙂

Leave a Reply

Notify of