简体   繁体   中英

How Get device orientation without compass sensor? Ionic 3

I'm developing an app that is required to get device orientation to know what the direction the users are going, even they are stopped. But many smartphones don't have the compass sensor, then I need to get the accelerometer and gyroscope data.

So, how can I get the device orientation using these sensors? I've already read some similar questions ( here ) , but it was applied to Android native, and it has a event.values that returns an array that I don't know how to get using Ionic plugins.

You can use ionic native Gyroscope plugin for this,

$ ionic cordova plugin add cordova-plugin-gyroscope
$ npm install --save @ionic-native/gyroscope

You can use the Platform service.

Important : orientation checking must be done after the platform becomes ready. Sample code in a .ts file:

import { Platform } from 'ionic-angular';

@Component({...})
export MyPage {
  constructor(public platform: Platform) {

      this.platform.ready().then(() => {
            if (this.platform.isPortrait())
                // Do something

            if (this.platform.isLandscape())
                // Do something
      });

  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM