Ionic get current location

ionic 2 get current location works once we restart our android device, please help me to fix this issue.

Check on this tutorial, hope it helps. Native Google Maps and Geolocation ionic

Thank you, i tried it but couldn’t fix the problem. It shows lang & lat as ‘undefined’.

Could you share your code as it will help me or someone assist you.

Code :

locationAt(){
this.geolocation.getCurrentPosition().then(res=>{

     let location='lat '+res.coords.latitude+' lang '+res.coords.longitude;
  
    let toast = this.toastCtrl.create({
    message: location,
    duration: 3000
    });
    toast.present();

});
}

I just run your code and it works fine. I use this as a guide - Geolocation

My .ts file

import { Component } from '@angular/core';
import { NavController, NavParams, ToastController } from 'ionic-angular';
import { Geolocation } from '@ionic-native/geolocation';

@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {

  constructor(
    public navCtrl: NavController, 
    public navParams: NavParams, 
    public geolocation: Geolocation,
    public toastCtrl: ToastController) {
  }
  
  ionViewDidLoad() {
    this.getLocation();
  }

  getLocation(){
    this.geolocation.getCurrentPosition().then((res) => {
      // resp.coords.latitude
      // resp.coords.longitude
      //let location= 'lat'+ res.coords.latitude +'lang'+ res.coords.longitude;
      let location='lat '+res.coords.latitude+' lang '+res.coords.longitude;
      let toast = this.toastCtrl.create({
        message: location,
        duration: 3000
      });
      toast.present();

    }).catch((error) => {
    console.log('Error getting location', error);
    });
  }
}

Install the Cordova and Ionic Native plugins:

$ ionic cordova plugin add cordova-plugin-geolocation --variable GEOLOCATION_USAGE_DESCRIPTION="To locate you"
$ npm install --save @ionic-native/geolocation
2 Likes

It’s working fine in windows platform. but it’s not working in android device after generating apk.

same problem with me not work on android device :frowning:

try with command - ionic cordova run android --prod

Its working for me thanks bro :slight_smile:

It is not working in live application as well as run app through android studio in android
In error giving blank.
“Error getting location ---- {}”, source: file:///android_asset/www/build/main.js (384)

Please refer this coding its must work,but you can import plugins in geolocation,geocoder.Then import app.module.ts file,then where you want to use this coding copy and paste it.

//get location coding this file you can paste it.
import { NativeGeocoder, NativeGeocoderReverseResult, NativeGeocoderOptions } from ‘@ionic-native/native-geocoder’;

@IonicPage()
@Component({
selector: ‘page-register’,
templateUrl: ‘register.html’,
})
export class RegisterPage {
//declare the variable
data:any = {};
public deviceInfo: deviceInterface = {};
loading: any;
public lat:number;
public lng:number;

//parameters are used to geolocation.
constructer(public nativeGeocoder: NativeGeocoder,public geolocation: Geolocation){
}
//Get a location
this.geolocation.getCurrentPosition({enableHighAccuracy: true}).then((resp) => {
this.lat= resp.coords.latitude;
this.lng= resp.coords.longitude;
console.log(this.lat);
console.log(this.lng);
this.reverseGeocoding(this.lat,this.lng);
}).catch((error) => {
console.log(‘Error getting location’, error);
});
}

//lat and lng conversion of original address.
reverseGeocoding(lat,lng){
let options: NativeGeocoderOptions = {
useLocale: false,
maxResults: 2
};
this.nativeGeocoder.reverseGeocode(lat,lng,options)
.then((result: NativeGeocoderReverseResult) =>{
this.data.addrss= result[1].subLocality +","+ result[1].locality +","+
result[1].subAdministrativeArea +","+ result[1].administrativeArea +"-"+
result[1].postalCode +","+result[1].countryName;
console.log(this.data.addrss);
alert(JSON.stringify(this.data.addrss));
}).catch((error: any) => console.log(error));
}
its work correctly,please refer this coding,try it.