2017-05-18 15 views
1

我有一個ionic2的應用程序,我想用陀螺離子本地和科爾多瓦-插件陀螺這樣tutoriel:ionic2離子本地和科爾多瓦-插件陀螺儀誤差

https://ionicframework.com/docs/native/gyroscope/

,但我有這個錯誤

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'watch' of undefined 
TypeError: Cannot read property 'watch' of undefined 
    at Observable._subscribe 

...

import { Component } from '@angular/core'; 
import { NavController } from 'ionic-angular'; 
import { Gyroscope, GyroscopeOrientation, GyroscopeOptions } from '@ionic-native/gyroscope'; 

import { Platform } from 'ionic-angular'; 
import {geoCompassService} from '../../services/geocompass' 
import * as Leaflet from "leaflet"; 

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

    pointGps:any; 
    angle:any; 
    distance:any; 
    orientation:any; 

    constructor(private platform: Platform, 
       public navCtrl: NavController, 
       public _geoCompassService:geoCompassService, 
       private gyroscope: Gyroscope) { 

     platform.ready().then(() => { 
     console.log("platform ready !!!"); 
      this.pointGps=this._geoCompassService.getPointGps(); 
      this.angle = (Math.atan2(0,1) - Math.atan2((this.pointGps.coordB.lng - this.pointGps.coordA.lng),             (this.pointGps.coordB.lat - this.pointGps.coordA.lat)))*180/Math.PI+180;  
      this.distance = Leaflet.latLng(this.pointGps.coordA).distanceTo(this.pointGps.coordB); 




      //Gyro 
      let options: GyroscopeOptions = { 
      frequency: 1000 
      }; 

      this.gyroscope.getCurrent(options) 
         .then((orientation: GyroscopeOrientation) => { 
       this.orientation=orientation 
       console.log("orientation "+this.orientation.x) 
      }) 
      .catch() 

      this.gyroscope.watch() 
          .subscribe((orientation: GyroscopeOrientation) => { 
       this.orientation=orientation 
      }); 


     }); 

    } 

} 

你能不能幫我

+0

能否請你加你使用的是什麼離子/離子原生的版本,和你的代碼的最相關的部分? – sebaferreras

+0

我使用@ ionic-native/core @ 3.9.2 ionic 2.2.1 – fansz

+0

您是否將'Gyroscope'設置爲提供程序? –

回答

0

請注意這個插件只能在真實的硬件上工作,假設你的錯誤是可見的,你正在使用瀏覽器而不是仿真器或真實設備。

import { Component } from '@angular/core'; 
import { Gyroscope, GyroscopeOrientation, GyroscopeOptions } from '@ionic-native/gyroscope'; 
import {Platform} from "ionic-angular"; 

@Component({ 
    selector: 'off-gyroscope', 
    templateUrl: 'gyroscope.html' 
}) 
export class GyroscopeComponent { 

    options: GyroscopeOptions = { 
    frequency: 100 
    }; 
    orientation: GyroscopeOrientation; 
    x: string; 
    y: string; 
    z: string; 

    constructor(public plt: Platform, private gyroscope: Gyroscope) { 
    plt.ready().then((readySource) => { 

     if (readySource == 'cordova') { 
     this.gyroscope.watch(this.options) 
      .subscribe((orientation: GyroscopeOrientation) => { 
      this.orientation = orientation; 
      this.x = orientation.x.toString(); 
      this.y = orientation.y.toString(); 
      this.z = orientation.z.toString(); 
      }); 
     } 
     else { 
     this.x = 'Not a real device'; 
     this.y = 'Not a real device'; 
     this.z = 'Not a real device'; 
     } 
    }); 
    } 
} 

關鍵的東西是readySource檢查任一dom(瀏覽器)或cordova(仿真器/裝置)。

不幸的是,文檔缺少清晰的提示,初學者如何處理'硬件'插件。 使用瀏覽器中使用的應用程序查看圖像。

App in the browser