2

When obtaining the magnetic north vector from the magnetometer on iOS there are three ways I know of to do it. Only the first method appears to give reasonable results. The phone is flat on it's back and it's long axis is pointing 97° E according to the Compass app.

  1. CLLocationManager delegate overload didUpdateHeading. This gives CLHeading with (x,y,z) vector of (-18.4, -15.3 -44.9) along with other helpful items such as magnetic/true heading, accuracy, timestamp.

This seems reasonable!

  1. CMMotionManager startMagnetometerUpdates. This gives CMMagneticField with (x,y,z) vector of (36.5, -10.1, -375). To be clear, the Z-axis value is negative three hundred and seventy five microteslas. This does not seem reasonable. If I rotate the phone the z value fluctuates, but not below -283 and that's when the phone is inverted from where it provided the result above.

Is this the internal magnetic field that has not yet been filtered out?

  1. CMMotionManager startDeviceMotionUpdates. This gives CMCalibratedMagneticField with property field with (x,y,z) vector of (0.0, 0.0, 0.0). This seems to be returning no meaningful data, although the events are firing rapidly and consistently.

Why isn't this returning any values?

Luke Pighetti
  • 4,541
  • 7
  • 32
  • 57

1 Answers1

1

You seem to be right about CMMagneticField the Apple docs specify that it includes "bias introduced from the device itself and its surroundings."

As far as I can tell, when CMMotionManager returns a vector of (0.0, 0.0, 0.0) that means it is not properly calibrated. You can check the accuracy value to make sure of this. A useful guide to calibrating your device can be found in this StackOverflow answer.

Note: the magnetic field returned by the CMMotionManager is not the same CMMagneticField returned by the magnetometer and therefore does not include the bias from the device. See the var magneticField: CMCalibratedMagneticField field within the docs.

  • Thank you! A question: how can `CLLocationManager` `didUpdateHeading` provide good, clean, accurate results if `CMMotionManager` `startDeviceMotionUpdates` is showing as not calibrated? I would expect them to share the same calibration. Do they have separate calibration profiles? – Luke Pighetti Sep 28 '20 at 12:35