Search code examples
iphonevectorcoordinatescllocationheading

iPhone - CLHeading: how can I transform the trueHeading to a vector


I know how to get the coordinates of the magnetic heading: heading.x, heading.y, heading.z The thing is that I'd need the (x, y, z)-vector of the trueHeading. How can I create this vector?

Thank you!


Solution

  • Edit: I have changed my answer quite a bit...

    Basically you need to rotate the magnetic north vector in the opposite direction to the Magnetic Declination angle.

    The hard part is that you need to rotate the vector on a horizontal plane. For that you need to know the orientation of the phone.

    Here is what you need to do:

    1. Get the magnetic north vector.

    2. Get the gravity vector from the accelerometer.

    3. Now calculate / look up the Magnetic Declination (it depends where you are in the world and it also varies slowly with time).

    4. Rotate the magnetic north vector X degrees about the gravity vector (where -X = Magnetic Declination). This will be the tricky part, you will need to brush up on some 3d trig.