In this tutorial, you will learn, how to enable location programmatically using the Ionic 4 Location Accuracy plugin. Location plays a very import role in many applications. Online delivery and e-commerce apps mostly rely on the location. This improves the user experience. If you take the popular raid hailing applications such as uber, ola, etc,  their application mainly relying on the location of users and drivers. So getting the location of the device is very important for many applications. New startups also want to give the same experience to the user from their app.

You can get the location information such as latitude and longitude using the Ionic 4 Geolocation plugin. However, the challenge is, by default, no one enables the location on the device. Telling users, go to settings and enable location also creates a big problem. Why because most of the people often failed to find, where the location settings are there and how to enable it. This will reduce the customer foot-falls to your app.

In this tutorial, I will explain, how to address the above-mentioned problem using the Ionic 4 framework with Location Accuracy plugin. The Location accuracy plugin will help us to enable the location programmatically without leaving Ionic 4 application. If you are looking creating an Ionic 4 mobile application and if your application is trying to use the location service and you are looking to enable the location service automatically, then this post is your right choice.

Android Preview of Location Accuracy Plugin in Ionic 4.

 

Let's do the automatically location enable service from scratch using the Ionic 4 and Location Accuracy plugin with code examples.

Step1.

Create a new Ionic 4 project using the below command.

ionic start crm blank
cd crm

 

Step2.

Now Install the Location Accuracy plugin using the below command on your terminal or command prompt.

ionic cordova plugin add cordova-plugin-request-location-accuracy
npm install @ionic-native/location-accuracy

Step3.

If you want to use the location accuracy plugin of Ionic 4, then you must put an entry of your plugin in the providers of the app.module.ts files. So import the LocationAccuracy class in the app.module.ts file like below.

import { LocationAccuracy } from '@ionic-native/location-accuracy/ngx';

Then add the Location Accuracy class to the provider like below on the app.module.ts file.

provider:[LocationAccuracy]

Step4.

Now import the LocationAccuracy class to home.page.ts file. We are going to use the location accuracy plugin on the home page. That's why I just import the LocationAccuracy class to the home.page.ts file. If you are using it some other page, then you must import the LocationAccuracy class to the corresponding pages.

import { LocationAccuracy } from '@ionic-native/location-accuracy/ngx';

Then create an object reference to the location accuracy class in the constructor like below.

constructor( private locationAccuracy: LocationAccuracy )
{
}

Now call the request() function from the location accuracy object reference and pass REQUEST_PRIORITY_HIGH_ACCURACY as a parameter. This request() function will show an alert box to enable the location.

enableLocation(){

    // the accuracy option will be ignored by iOS
    this.locationAccuracy.request(this.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY).then(
      () => {
        console.log('Request successful')
      
      },
      error => {
        console.log('Error requesting location permissions', error)
      }
    );
  
}

Step5.

Create a  button on the home.page.html file and call the above-mentioned function using the click event.

<ion-button (click)="enableLocation()" expand="block">Enable Location</ion-button>

That's all. All are done. Now you can test this on your Android device. First, add the Android platform using the below command.

ionic cordova platform add androd

Then connect your device via USB and run the following command to install this apk to your device.

ionic cordova run android

That's all. I hope this tutorial will help you to enable the location programmatically using the Location Accuracy plugin on your Ionic 4 project. All codes are tested on my device and releasing it as a tutorial. However, if you found any bug, you can report it to my git repo.

Downnload APK

Get the Location Accuracy APK and test it on your Android Device.

https://github.com/bharathirajatut/ionic4/blob/master/location-accuracy-example/app-debug.apk

Tested Device: Moto G4 Plus

You can download this apk and test it on your device. This apk is not harmful and it is not stealing any info. If you wanna try, please download install on your device. And you can try this without any worries.

Get the full source code using the git.

https://github.com/bharathirajatut/ionic4/tree/master/location-accuracy-example