How to get Android phone orientation matching human orientation?

后端 未结 4 1378
有刺的猬
有刺的猬 2021-01-31 17:30

I\'m making a map app, including the location arrow that shows you which way you\'re facing, like so:

\"Map

4条回答
  •  一生所求
    2021-01-31 18:06

    You should take the pitch and determine if the user is close to holding the phone vertically up.

    i chose that after 45 degree pitch up or down from flat on table, the coordinate system should be remapped.

    if (Math.round(Math.toDegrees(-orientation[1])) < 45 && Math.round(Math.toDegrees(-orientation[1])) > -45) {
    //do something while the phone is horizontal
    }else{
    //R below is the original rotation matrix
    float remapOut[] = new float[9];
    SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X, SensorManager.AXIS_Z, remapOut);
    //get the orientation with remapOut
    float remapOrientation[] = new float[3];
    SensorManager.getOrientation(remapOut, remapOrientation);
    

    It has worked out pretty well. Let me know if anyone can suggest an improvement on this. Thanks.

提交回复
热议问题