激活gps后,地理定位对象似乎是空的

时间:2018-03-30 10:37:14

标签: typescript geolocation ionic3 angular5 google-geocoder

我想显示移动应用用户的当前位置,当gps打开时,它会向我显示正确的位置。但是当它关​​闭然后按下按钮打开它时,它不会改变我的html中的值下面的变量值:

<span ion-text class="text-11x no-margin" color="primary">{{yourLocation ? yourLocation : 'Zet je locatievoorzieningen aan'}}</span>

当我点击此按钮时,gps已开启:

<button color="primary" (click)="activateGpsSettings()">

这是activateGpsSettings():

activateGpsSettings() {
    this.locationAccuracy.canRequest().then((canRequest: boolean) => {

        if (canRequest) {
            // the accuracy option will be ignored by iOS
            this.locationAccuracy.request(this.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY).then(
                () => {
                    console.log('turned On');
                    setTimeout(() => {
                        this.geoloactionDecode();
                    }, 500)
                },
                error => console.log('Error requesting location permissions', error)
            );
        }

    });
}

gealocationDecode():

 geoloactionDecode() {

    console.log('executed', this.geolocation);
    this.geolocation.getCurrentPosition().then((position) => {
        let lat = position.coords.latitude;
        var lng = position.coords.longitude;

        console.log('lat-lng', lat, lng);

        this.geocoderService.reverseGeocode(lat, lng)
            .then((data: any) => {
                this.geocoded = true;
                this.yourLocation = data;
                console.log('yourLocation:', this.yourLocation);
                // return this.yourLocation;
            })
            .catch((error: any) => {
                this.geocoded = true;
                this.yourLocation = error.message;
            });

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

这是地理编码服务:

  @Injectable()
export class GeocoderService {

   constructor(private geoCode  : NativeGeocoder){}

   /**
     *
     * Perform reverseGeocoding operation and return address details
     *
     * @public
     * @method reverseGeocode
     * @return {Promise}
     *
     */
   reverseGeocode(lat : number, lng : number) : Promise<any>
   {
      return new Promise((resolve, reject) =>
      {
         this.geoCode.reverseGeocode(lat, lng)
         .then((result : NativeGeocoderReverseResult) =>
         {
            console.log(result[0])
            let str : string   = `Je huidige locatie is: ${result[0].thoroughfare}, ${result[0].locality}`;
            resolve(str);
         })
         .catch((error: any) =>
         {
            console.log(error);
            reject(error);
         });
      });
   }
}

但是看起来很奇怪的是函数日志:执行Geolocation object,打开,executed empty Geolocation Object

像这样:Geolocation {}__proto__: IonicNativePlugin

第一个是因为它在构造函数中执行,而不是单击按钮时它在activateGpsSettings()中记录控制台日志而在execute方法中记录geoloactionDecode()日志。 / p>

我无法弄明白为什么它没有通过按下按钮进入this.geolocation.getCurrentPosition().then(),因为它打开了gps ......

0 个答案:

没有答案