无法读取属性'纬度'未定义的

时间:2017-08-08 07:06:14

标签: android cordova ionic2 cordova-plugins ionic-native

我正在尝试离子原生。获得许可是成功的,但是获取错误无法读取属性'纬度'未定义之后。

注意*我使用的是Android模拟器,而不是物理设备。

    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');
        }
    }

1 个答案:

答案 0 :(得分:1)

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