title: Compass in Android
body: <p>I am trying to program a compass with Android using the accelorometer and the magnetic field sensor, now I am wondering how to get the correct angle for my compass.</p>
  
  <p>I read in the values of the accelerometer and of the magnetic field sensor in "accele", and "magne" respectively. To get the angle, I perform the following:</p>
  
  <pre><code>float R[] = new float[9];
  float I[] = new float[9];
  boolean success = SensorManager.getRotationMatrix(R, I, accele, magne);
          if(success) {
              float orientation[] = new float[3];
              SensorManager.getOrientation(R, orientation);
              azimuth = orientation[0]; // contains azimuth, pitch, roll
                              ....
  </code></pre>
  
  <p>Later on, I use a rotation matrix to put my needle:</p>
  
  <pre><code>rotation.setRotate(azimuth, compass.getWidth() / 2, compass.getHeight() / 2);
  canvas.drawBitmap(needle, rotation, null);
  </code></pre>
  
  <p>Now, the documentation of getOrientation says, that orientation[0] should be the rotation around the z-axis. The documentation for TYPE_ORIENTATION states that
  "Azimuth, angle between the magnetic north direction and the y-axis, around the z-axis (0 to 359). 0=North, 90=East, 180=South, 270=West".</p>
  
  <p>My azimuth however is not between 0 and 359, but rather around -2 to 2. What exactly is the azimuth from getOrientation and how can I convert it to an angle?</p>