I was trying out the ionic native. Getting permission was successful, but getting the error cannot read property 'latitude' of undefined afterward.
Note* I am using the android emulator, not the physical device.
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { Geolocation } from '@ionic-native/geolocation';
import { AndroidPermissions } from '@ionic-native/android-permissions';
export class GeolocationPage {
latitude: number;
longitude: number;
constructor(private platform: Platform, private geolocation: Geolocation, private androidPermissions: AndroidPermissions) {
this.platform.ready().then(() => {
this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.ACCESS_COARSE_LOCATION]).then(
sucess => this.onSucess(),
err => this.onFailure()
);
});
}
onSucess(){
alert('onSucess');
this.geolocation.watchPosition().subscribe(pos => {
this.latitude = pos.coords.latitude;
this.longitude = pos.coords.longitude;
});
}
onFailure(){
alert('onFailure');
}
}