title: How to get Android phone orientation matching human orientation?
body: <p>I'm making a map app, including the location arrow that shows you which way you're facing, like so:</p>
  
  <p><img src="https://i.stack.imgur.com/o9DYE.png" alt="Map with a blue arrow"></p>
  
  <p>I get the orientation directly from <code>SensorManager.getOrientation()</code>, using the first returned value: azimuth. When the phone is held so that the screen is pointing above the horizon, and in portrait, the arrow works fine. However:</p>
  
  <ul>
  <li>When the phone is held so the screen is pointing below the horizon, the arrow points 180 degrees away from the direction the user is facing.</li>
  <li>When the phone is held so the screen's facing is level with the horizon, the arrow has no clue which way it's pointing. Azimuth does not return meaningful results at all.</li>
  <li>When the phone is tilted left or right (or held in landscape mode), the arrow tilts to the left or right.</li>
  </ul>
  
  <p>The carefully constructed and scientific image below shows what I mean (where blue is the user's facing, red is the arrow direction, the screen is approximately facing the user's face, and Google Maps does exactly what I want):</p>
  
  <p><img src="https://i.stack.imgur.com/meM21.png" alt="Graph of what happens vs what I want"></p>
  
  <p>(Note that, with Google Maps, it doesn't successfully do the last two actions on the list if auto-rotate is off. But I'm not even at that stage, yet. One thing at a time.)</p>
  
  <p>It appears as though it's simply using the Y axis pointing direction as shown here: <a href="http://developer.android.com/reference/android/hardware/SensorEvent.html" rel="noreferrer">http://developer.android.com/reference/android/hardware/SensorEvent.html</a>, when I want it to use the reverse of the Z axis pointing direction, most of the time, and the Y when the phone is flat. However, given the values that <code>getOrientation()</code> returns, I'd have to write complex cases to fix some of the issues, and the phone-facing-horizon use case is unsolvable. I'm certain there's an easier way.</p>
  
  <p>Here's my code (Where lastAcceleration and lastMagneticField both came from the internal sensor):</p>
  
  <pre><code>float[] rotationMatrix = new float[9];
  if(SensorManager.getRotationMatrix(rotationMatrix, null, lastAcceleration, lastMagneticField)){
      float[] orientMatrix = new float[3];
      SensorManager.getOrientation(rotationMatrix, orientMatrix);
  
      orientation = orientMat[0]*180/(float)Math.PI;
  }
  </code></pre>
  
  <p>What am I doing wrong? Is there an easier way to do this?</p>
  
  <p>Edit: Just to clarify, I'm making the assumption that the user is holding the device in front of them, and the screen is pointing towards them. Beyond that, I obviously can't tell if only one of them rotates. Also, I am using the motion of the user when they are moving, but this is for when they are stationary.</p>