Development

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

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.

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.

Published by
DevOps Guy

Recent Posts

How to Implement AI Data Management In Your Business

Have you ever wondered how businesses easily process enormous volumes of data, derive valuable insights,…

3 weeks ago

Developing Cloud Applications: The Complete Guide for 2025

Discover the steps for developing cloud applications, from costs to cloud app deployment

1 month ago

Why Build an MVP in 2025 | Video

Imagine launching your product with just the core features, getting honest user feedback, and then…

1 month ago

How CDN Reduced AWS Costs For MEXICANA Airlines Web App

When a tight deadline is non-negotiable, every second counts! Here’s how we developed and launched…

1 month ago

Nearshore Software Development Company | Complete Guide

You may have considered hiring a nearshore software development company or services, but you still have doubts…

2 months ago

End to End Project Management: Complete Guide

End-to-end project management goes as far back as you can remember. Every project in history, even…

2 months ago