android - Android中如何获取方向(如北、西)

标签 android android-manifest compass-geolocation

我是 Android 的新手,我想根据我的相机获取方向。如何根据我的相机获取方向信息?你能给出一个想法吗?

最佳答案

TYPE_ORIENTATION 已弃用

我们不能再使用方向传感器了,我们可以串联使用磁场传感器和加速计传感器来获得等效的功能。这需要更多工作,但它确实允许继续使用回调来处理方向更改。

加速度计和磁场方位角的转换:

float[] mGravity;
float[] mGeomagnetic;

public void onSensorChanged(SensorEvent event) {

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        mGravity = event.values;

    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
        mGeomagnetic = event.values;

    if (mGravity != null && mGeomagnetic != null) {
        float R[] = new float[9];
        float I[] = new float[9];

        if (SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic)) {
            
            // orientation contains azimut, pitch and roll
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);

            azimut = orientation[0];
        }
    }
}

要指向北方,您可以计算以度为单位的旋转:

float rotation = -azimut * 360 / (2 * 3.14159f);

关于android - Android中如何获取方向(如北、西),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8315913/

相关文章:

android - 旋转二维图像onclick android

android - 是否可以同时查看 Graphic layout 和 layout.xml?

android - 如何创建一个指针始终指向某个 GPS 位置的指南针? (在安卓系统中)

objective-c - 当在我的 iPhone 应用程序项目中使用指南针模式时,MKMapView 相反

Android 指南针示例

android - 在 kotlin 中连接两个字节数组的简单方法?

android - 关于android日期格式的问题

android - AOSP中哪里是解析AndroidManifest.xml的代码?

Android更改默认权限保护级别

android - ACTION_USER_PRESENT BroadcastReceiver 不能在 list 中注册吗?