How to use the Geolocation and LaunchNavigator plugins in your Ionic 3 project

How to use the Geolocation and LaunchNavigator plugins in your Ionic 3 project

For some of us, the geolocation ionic 3 plugin can be very useful. With it, we can obtain the user’s location and use it for different purposes in our projects. In this case, we are going to get the user’s location, store it in some variables and then use those variables to send them as parameters to the launch navigator plugin and launch the google maps app (if available) to show the route form the user’s location to a specific location.

1. First of all, we need to install it running this command:

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

2. Then we need to declare it in our app.module.ts file.

import { Geolocation } from '@ionic-native/geolocation';
 
providers: [
StatusBar,
SplashScreen,
Geolocation,
{provide: ErrorHandler, useClass: IonicErrorHandler} ]

3. We also need to declare it in the component we are going to use this plugin in:

import { Geolocation } from '@ionic-native/geolocation';
 
constructor(public navCtrl: NavController, public geolocation: Geolocation) {}

4. Now that we have declared our plugin we can start using it, and it is very easy to use.

import { NavController} from 'ionic-angular';
import { Geolocation } from '@ionic-native/geolocation';
 
@Component({
selector: 'page-geolocation',
templateUrl: 'geolocation.html'
})
 
export class GeolocationPage {
latitude:number ;
longitude:number ;
constructor(public navCtrl: NavController, public geolocation: Geolocation, private launchNavigator:LaunchNavigator) {}
ionViewDidLoad(){
this.geolocation.getCurrentPosition().then(position =>{
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude;
},error=>{
console.log('error',error);
});
}
}

What are we doing here? Well, we are declaring two number variables (latitude and longitude) to store the user’s latitude and longitude, then we are asking ionic if the view loaded with the function ionViewDidLoad, in case it did load, the code inside this function will be executed. Inside this function we used the geolocation method getCurrentPosition() which will provide us the coordinates(coords) or the timestamp of the user location. So we are getting the latitude and longitude from the coords.

Now let’s see how we can use this coordinates to open google maps showing the user’s location and the distance to a particular establishment using another Cordova plugin called launchNavigator.

5. We need to install the launchNavigator plugin with these commands.

  1. $ ionic cordova plugin add uk.co.worykingedge.phonegap.plugin.launchnavigator
  2. $ npm install –save @ionic-native/launch-navigator

6. And as we did with the geolocation plugin we have to declare it in our app.module.ts file and in our constructor.

import { Geolocation } from '@ionic-native/geolocation';
import { LaunchNavigator } from '@ionic-native/launch-navigator';
 
providers: [
StatusBar,
SplashScreen,
Geolocation,
LaunchNavigator,
{provide: ErrorHandler, useClass: IonicErrorHandler} ]

7. Also we have to declare it in our component.

import { LaunchNavigator, LaunchNavigatorOptions } from '@ionic-native/launch-navigator';
constructor(public navCtrl: NavController, public geolocation: Geolocation,private launchNavigator:LaunchNavigator) {}

8. Okay, so let’s get to it

navigateLocation(){
let options: LaunchNavigatorOptions = {
app: this.launchNavigator.APP.GOOGLE_MAPS,
start:[this.latitude,this.longitude]
};
this.launchNavigator.navigate('London, ON',options)
.then(success =>{
console.log(success);
},error=>{
console.log(error);
})
}

We created a function called navigateLocation, in which we make use of our launchNavigator plugin. In this function we are declaring the launchNavigator options which are “app” (the name of the app we are launching, in this case, Google Maps) and “start” (the coordinates of the user we obtained with the geolocation plugin), then let’s say we want to navigate to London. So we call the navigate method and send the destination, in this case, London, and the launchNavigator options. Lastly we call the method then and we console log the success and error message.

9. At the end your component should look like this

  1. import { NavController} from ‘ionic-angular’;
  2. import { Geolocation } from ‘@ionic-native/geolocation’;
  3. import { LaunchNavigator, LaunchNavigatorOptions } from ‘@ionic-native/launch-navigator’;
  4.  
  5. @Component({
  6. selector: ‘page-geolocation’,
  7. templateUrl: ‘geolocation.html’
  8. })
  9.  
  10. export class GeolocationPage {
  11. latitude:number ;
  12. longitude:number ;
  13. constructor(public navCtrl: NavController, public geolocation: Geolocation, private launchNavigator:LaunchNavigator) {}
  14. ionViewDidLoad(){
  15. this.geolocation.getCurrentPosition().then(position =>{
  16. this.latitude = position.coords.latitude;
  17. this.longitude = position.coords.longitude;
  18. },error=>{
  19. console.log(‘error’,error);
  20. });
  21. }
  22. navigateLocation(){
  23. let options: LaunchNavigatorOptions = {
  24. start:[this.latitude,this.longitude],
  25. app: this.launchNavigator.APP.GOOGLE_MAPS
  26. };
  27. this.launchNavigator.navigate(‘London, ON’, options)
  28. .then(success =>{
  29. console.log(success);
  30. },error=>{
  31. console.log(error);
  32. })
  33. }
  34. }

10. Now in order for us to use this, we must have a button which calls this method

<ion-header>
<ion-navbar>
<ion-title>
Geolocation
</ion-title>
</ion-navbar>
</ion-header>
<ion-content>
<button (click)="navigateLocation()">Navigate google maps</button>
</ion-content>

At the end you’ll have something like this.

Geolocation

Because ionic 3 is buit on top of apache cordova, you can find tons of plugins like these in the ionic 3 documentation https://ionicframework.com/docs/native and they are very easy to use.
In ClickIT we implement technologies like these plugins Cordova provides, to give a high-quality service to our customers.

Develop your brilliant app idea

Subscribe

to our newsletter

Table of Contents

We Make DevOps Easier

Weekly DevOps Newsletter

Subscribe to our DevOps News

Subscribe to a monthly newsletter to receive the IT best practices, startup-related insights & emerging technologies.

Join hundreds of business leaders and entrepreneurs, who are part of our growing tech community.

We guarantee 100% privacy. Your information will not be shared.