×

Ionic Geolocation

In today’s time, the mobile location plays an essential role in the mobile application. The Ionic Cordova geolocation plugin provides information about the location of the device, like as latitude and longitude. The most common sources to get the location information of devices are Global Positioning System (GPS) and location derived from network signals like as IP addresses, Radio Frequency Identification (RFID), GSM/CDMA cell IDs, and Wi-Fi and Bluetooth MAC addresses.

The geolocation API is based upon the specifications of the W3C Geolocation API. It runs only on devices that do not provide an implementation. It supports different platforms such as Android, Browser, iOS, Windows, and Amazon Fire OS.

Nowadays, the most popular mobile applications, like WhatsApp, Twitter, Facebook, and Instagram, use geolocation plugin to share the user’s location. Some apps give the riding, food, E-commerce services based on the geolocation.

In this topic, we are going to discuss the ionic geolocation. Here, you will see step by step all the process of ionic geolocation in the ionic application.

Step 1. Firstly, you create a new project in the ionic application. It also makes sure that you have installed the latest version of the ionic framework.

Step 2. After the installation of the latest version of the ionic framework, you are required to select a blank template from the various templates. After this process, you need to run the following commands to install the Cordova geolocation plugin in the ionic application. 

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

Step 3. After executing these commands, it is required to import the geolocation plugin in the app.module.ts file, as shown below:

App.module.ts

 import { NgModule } from '@angular/core';  
 import { BrowserModule } from '@angular/platform-browser';  
 import { RouteReuseStrategy } from '@angular/router';  
 import { IonicModule, IonicRouteStrategy } from '@ionic/angular';  
 import { SplashScreen } from '@ionic-native/splash-screen/ngx';  
 import { StatusBar } from '@ionic-native/status-bar/ngx';  
 import { AppComponent } from './app.component';  
 import { AppRoutingModule } from './app-routing.module';  
 import { Geolocation } from '@ionic-native/geolocation/ngx';   
 @NgModule({  
   declarations: [AppComponent],  
   entryComponents: [],   
   imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],  
   providers: [  
     StatusBar,  
     SplashScreen,  
     Geolocation,    
     { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }  
   ],  
   bootstrap: [AppComponent]  
 })  
 export class AppModule {}   

Step 4. In this step, you are required to import geolocation plugin in the home.page.ts file. Here, you create a function to find the location of the device, as shown below:

Home.page.ts

import { Component } from '@angular/core';  
 import { Geolocation } from '@ionic-native/geolocation/ngx';  
 import { LoadingController } from '@ionic/angular';  
 import { AlertController } from '@ionic/angular';  
 @Component({  
   selector: 'app-home',  
   templateUrl: 'home.page.html',   
       message: 'Please wait.....',  
       });  
     await loading.present();  
     this.geolocation.getCurrentPosition({maximumAge: 1500, timeout: 6000, enableHighAccuracy: true }).then((resp) => {  
       // resp.coords.latitude  
       // resp.coords.longitude  
       loading.dismiss();  
       this.lati = resp.coords.latitude;  
       this.longi = resp.coords.longitude;  
       }, er => {  
         // alert("error getting location")  
         loading.dismiss();   
         this.showLoader('Can not retrieve Location');  
       }).catch((error) => {  
       // alert('Error getting location'+JSON.stringify(error));   
       loading.dismiss();  
       this.showLoader('Error getting location - ' + JSON.stringify(error));  
       });  
   }  
  async showLoader(msg) {  
     const alert = await this.alertController.create({   
       message: msg,  
       buttons: ['OK']  
     });  
     await alert.present();  
   }  
 }   

Step 5. In this step, you write the code in the home.page.html file and also create a button to get the location of device, as shown below:

Home.page.html

<ion-header>  
   <ion-toolbar color="success">  
     <ion-title>Ionic Geolocation Example</ion-title>  
   </ion-toolbar>   
 </ion-header>  
 <ion-content class="padding">  
   <ion-button (click)="getCurrentLocation()" expand="block">Search Location</ion-button>  
   <ion-list>  
     <ion-list-header>  
       <ion-label>Location Details</ion-label>  
     </ion-list-header>  
     <ion-item>   
       <ion-label>Longitude</ion-label>  
       <ion-badge>{{longi}}</ion-badge>  
     </ion-item>  
     <ion-item>  
       <ion-label>Latitude</ion-label>  
       <ion-badge>{{lati}}</ion-badge>  
     </ion-item>  
   </ion-list>   
 </ion-content>   

Step 6. In this step, you can use your android device to run the app. Before testing the app, you required to add the platform to the app. The following command helps to install the platform with android, as shown below:

$ ionic cordova platform add android
 $ ionic cordova run android –aot 

Output: After successful execution of the code, you will get the following output:

Ionic Geolocation

When you click on the Search Location button, it shows the latitude and longitude information of the location, as shown below:

Ionic Geolocation

Related Topics

Ionic Grid

The Ionic grid is a very powerful mobile-first flexbox system for building the custom layouts. It is a CSS feature supported by all devices that ionic supports. The Ionic grid is composed of...

11 minutes read.

Ionic Cards

Ionic Cards: Ionic cards are a standard part of the user interface that acts as an entry point for more specific information. These are the best components for displaying information...

4 minutes read.

Ionic vs Apache Cordova

Ionic Vs Apache Cordova Ionic and Cordova are two popular technology going on in the market for mobile application development. The combination of these two technologies creates a development platform that is known as...

3 minutes read.

Ionic Input

Ionic Input Inputs are necessary to collect and process user inputs securely. To be easy for users to communicate, they must follow the process and communication guidelines for each platform. It is a...

3 minutes read.

Ionic Editors

Editors are software programs that permit the users to create and edit the text files. In the development field, editors are usually used to create the source code editors that includes many specific...

3 minutes read.

Ionic Geolocation

In today’s time, the mobile location plays an essential role in the mobile application. The Ionic Cordova geolocation plugin provides information about the location of the device, like as latitude and longitude. The...

5 minutes read.

Features of Ionic Framework

The Ionic Framework is based on the AngularJS framework that permits a programmer to use a combination of many other programming languages, like HTML5, CSS, and JavaScript. Some of the...

4 minutes read.

Ionic Tutorial for Beginners

What is Ionic? Ionic is an open-source framework that is used for the development of mobile applications. It is also used to build android applications, iOS, applications, desktop applications, and web applications using web...

4 minutes read.

Ionic Alert

Ionic Alert An Alert is a dialog box that presents the users with important information, or it stores the data from the user by using the inputs. Alerts are a great way to provide...

5 minutes read.

Ionic Spinner

The ionic spinner component provides the various SVG (Scalable Vector Graphic) spinners. Ionic spinners are the visual indicators that display the app which is loading content or performing another process that the user...

5 minutes read.

Ionic Splash Screen

The Apache Cordova plugin helps to displays and hides a splash screen during the application launch. The Ionic Splash Screen is similar to a home page on a website. The ionic splash...

2 minutes read.

Ionic Buttons

Ionic Buttons There are different types of buttons in the Ionic framework. These buttons are subtly animated, enhancing the user experience when using the application. Buttons are a fundamental way of interacting and navigating...

4 minutes read.

Ionic Toolbar

A toolbar is a general toolbar that can be used in an application such as a header, sub-header, footer, or even a sub-footer. Because the ion-toolbar is flexbox-dependent, no matter how many toolbars...

3 minutes read.

Ionic Slides

The ionic slides component is the multi-section container. You can swipe or drag any segment between them. This contains any number of slides components. Slides help it easy to make galleries, tutorials, and...

2 minutes read.

Ionic Date Time

Ionic Date Time The DateTime component is used to display an interface that makes it simple for users to select dates and times. Tapping on <ion-datetime> will display a slider up from the...

4 minutes read.

Ionic Range

The Range slider lets users choose from a range of values by moving the slider knob. You can accept two knobs, but by default, one-knob controls the range value. Range labels You can put down on...

2 minutes read.

Ionic Contents

Ionic Contents The content component provides an easy-to-use content area with some useful ways to control the scrollable region. There should be only one content in one view. Content, along with several other Ionic...

2 minutes read.

Ionic Modal

Ionic modal is displayed as a temporary UI that slides into the screen. The ionic modal is a dialog box that is displayed at the top of the app’s content, and it...

5 minutes read.

Difference between Ionic 4 and Ionic 3

The ionic framework is an open-source platform that is used to develop mobile applications. It is also used to build android applications, desktop applications, iOS, and web applications using web technologies like HTML,...

5 minutes read.

Ionic Chip

Ionic chip displays the complex entities in small entities in small blocks, such as the contacts. Ionic chip has several types of elements like avatars, thumbnails, text, and icons. It is a bubble-like...

3 minutes read.