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.

Disqus Comments Loading...
Published by
DevOps Guy

Recent Posts

Web Application Architecture: The Latest Guide 2024

When a user logs on to this desktop/laptop or mobile, opens a browser and types…

16 hours ago

Low-Code Development for Business Success

Low-code development is great news for businesses, reducing time-to-market for apps, and allocating costs more…

3 days ago

PHP Latest Versions Guide | Video

So, have you caught wind of the latest PHP update? If you not, this PHP…

2 weeks ago

ECS vs EC2: Choosing the Right AWS Service for Your Workloads

When it comes to AWS ECS vs EC2, the choice boils down to your specific…

2 weeks ago

Netflix Architecture | A Look Into Its System Architecture

Ever wondered how Netflix keeps you glued to your screen with uninterrupted streaming bliss? Netflix…

3 weeks ago

Snowflake vs Redshift: Key Differences

In today's busy world, where information is important, handling data well is crucial for success.…

1 month ago