ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • Firebase Cloud Messaging (FCM) using Cordova FCM Plugin
  • Cordova Upload Images using File Transfer Plugin and .Net core WebAPI
  • Could not find gradle wrapper within Android SDK. Might need to update your Android SDK. Looked here: Local\Android\Sdk\tools\templates\gradle\wrapper
  • What is ionic? How to start mobile app development with ionic?
  • How to make the two side-menu in ionic?
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

How to get Current user Location from device in ionic Cordova?

Hybrid Development Ionic about 7 years ago || 11/11/2018 || 8.1 K View

  • Hide

Getting the user's current location from the device is easy when you find the best plugin. I tried many plugins but couldn't succeed.  Finally, I found two plugins.

Geo Location (Cordova-plugin-geolocation)

This plugin gives us the device’s current location such as latitude and longitude. Common sources of location information include Global Positioning System (GPS) and location inferred from network signals such as IP address, RFID, WiFi and Bluetooth MAC addresses, and GSM/CDMA cell IDs. This API is based on the W3C Geolocation API Specification and executes only on devices that don't already provide an implementation.

 Native Geo Location (cordova-plugin-nativegeocoder)

This plugin converts the latitude and longitude into the address or converts the location into latitude and longitude.

That’s it. We can get the current user location from using these two plugins.

So, let’s start. We assume we have set our development environment in our machine (desktop or laptop).

ionic start myuserlocation blank

cd myuserlocation

Install the Geolocation plugin and Native Geo Location. Type following command and Hit Enter

Geolocation plugin

ionic cordova plugin add cordova-plugin-geolocation --variable GEOLOCATION_USAGE_DESCRIPTION="To locate you"

npm install --save @ionic-native/geolocation

Native Geolocation plugin

ionic cordova plugin add cordova-plugin-nativegeocoder

npm install --save @ionic-native/native-geocoder

After installing these two plugins now register in app.module.ts in provider section

  providers: [

    StatusBar,

    Geolocation,

    NativeGeocoder,

    SplashScreen,

    {provide: ErrorHandler, useClass: IonicErrorHandler}

  ] 

Place these two lines in the top. 

import { Geolocation } from '@ionic-native/geolocation';

import { NativeGeocoder } from '@ionic-native/native-geocoder';

 Add this in home.ts Constructer

constructor(

public navCtrl: NavController,

private geolocation: Geolocation,

private nativeGeocoder: NativeGeocoder

) { }

Create the Function in home.ts  as getUserLocation()

getUserLocation(){
    let options: NativeGeocoderOptions = {
      useLocale: true,
      maxResults: 5
    };
    this.geolocation.getCurrentPosition().then(resp => {
      this.nativeGeocoder.reverseGeocode(resp.coords.latitude, resp.coords.longitude, options)
        .then((result: NativeGeocoderReverseResult[]) => {
          this.userlocation = result[0].toString();
        }, error => {
          console.log(error)
        });
    }, error => {
      console.log('Error getting location', error);
    })
  }

 In home.html paste this line.

<button ion-button (click)="getUserLocation()">Get location</button>
<h1>Current user Location is {{userlocation}}</h1>

After registering plugins in provider section and Create the function for Location. We need to Add the Cordova android in our project because this plugin is not working on emulator and browser.

Type following command and hit enter

Ionic cordova platform add android  

After adding the platform let run on the device. Type the following command and hit Enter

Ionic cordova run android

Screen Shot from my device

 

 

My development details are below:

Ionic:

   ionic (Ionic CLI)  : 4.1.2

   Ionic Framework    : ionic-angular 3.9.2

   @ionic/app-scripts : 3.2.0

Cordova:

   cordova (Cordova CLI) : 8.1.1 (cordova-lib@8.1.0)

   Cordova Platforms     : android 7.1.2

   Cordova Plugins       : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 2.2.3,   (and 6 other plugins)

System:

   Android SDK Tools : 26.1.1

   NodeJS            : v8.11.4

   npm               : 5.6.0

   OS                : Windows 10

  • 2
  • 0
  • 0
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Anil Shrestha

Anil Shrestha

Qualified and skilled professional with experience working as a Software Developer

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey