title: How can I calculate the heading (N/W/S/E) given (x,y,z) magnetometer and accelerometer data?
body: <p>I am using <a href="https://react-native-sensors.github.io/docs/API.html" rel="nofollow noreferrer">react-native-sensor</a> to grab the raw data from these sensors.</p>
  
  <pre><code>import {magnetometer, acclerometer} from 'react-native-sensors';
  const subscription = accelerometer.subscribe(({ x, y, z, timestamp }) =&gt;
      console.log({ x, y, z, timestamp })
      this.setState({ accelerometer: { x, y, z, timestamp } })
  );
  const subscription = magnetometer.subscribe(({ x, y, z, timestamp }) =&gt; 
      console.log({ x, y, z })
      this.setState({ magnetometer: { x, y, z, timestamp } })
  );
  </code></pre>
  
  <p>Given these 6 data points, how can I get the degree and direction? What is the appropriate algorithm? </p>
  
  <p>I do not understand the algorithm in this <a href="https://stackoverflow.com/questions/18112729/calculate-compass-heading-from-deviceorientation-event-api">answer</a>.  This answer utilizes alpha, beta, gamma...is that the same as "x, y, z"?  Why does that only use 3 data points and not 6? Why do some <em>other</em> answers say that accelerometer data is required (for tilt adjustment?).  Why isn't there an answer that utilizes all 6 data points?</p>
  
  <p>(note: the documentation has a mis-spelling of "magenetometer")</p>