简体   繁体   English

如何在没有指南针传感器的情况下获取设备方向? 离子3

[英]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. 我已经读过一些类似的问题( 在此处 ),但是它已应用于Android本机,并且具有event.values返回一个我不知道如何使用Ionic插件的数组。

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: .ts文件中的示例代码:

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

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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