2

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');
        }
    }
Anil8753
  • 2,663
  • 4
  • 29
  • 40
  • Use real device if you don't have any GPS emulator. If you don't have it. check the another answers..https://stackoverflow.com/questions/2279647/how-to-emulate-gps-location-in-the-android-emulator and https://stackoverflow.com/questions/38247350/emulators-location-simulation-not-working – tommybee Aug 08 '17 at 07:14
  • can you console log `pos` ? – Suraj Rao Aug 08 '17 at 07:23

1 Answers1

1
 onSucess(){
     alert('onSucess');
     let _me = this
     this.geolocation.watchPosition().subscribe(pos => {
                 _me.latitude =  pos.coords.latitude;
                 _me.longitude = pos.coords.longitude;
             });
         }
Shing
  • 121
  • 7