Heading

The location manager and its delegate can deliver our heading as well as our location.

Heading is the direction that we’re facing. It can be measured with respect to magnetic north or true north. In Manhattan, the compass needle points about 13° to the left of true north; I looked it up here. This 13° difference is called the magnetic declination. The avenues point 29° to the right of true north; I looked it up here. The needle therefore points about 42° to the left of the avenues. Do not follow a compass needle when trying to walk uptown.

The instance variable magneticNorth contains the direction in which the top of the iPhone is pointing, measured in degrees to the right of magnetic north: 0° for north, 90° for east, 180° for south, etc. We use the top of the iPhone because we set the headingOrientation property of the location manager to CLDeviceOrientationPortrait.

The variable theta (θ) is measured in radians to the left of east: 0 radians for east, π/2 radians for north, π radians for west.

Source code in Heading.zip

  1. main.m
  2. Class HeadingAppDelegate
  3. Class View
  4. Property list Heading-Info.plist

Create the project

Add the Core Location framework to the project as we did here. Add the UIRequiredDeviceCapabilities key to the Heading-Info.plist file with the value magnetomter.

Things to try

  1. Draw a sector (pizza slice) instead of a line for magnetic north, showing the accurracy.

  2. Show true north too, if it’s available. Since the declination depends on where you are (13° in New York, 15° in Boston), the true north is available only if the location managercan figure out where we are. See the locationServicesEnabled variable of class LLocationManager.

  3. Draw the vector showing the direction of x and y instance variables, which are magnitudes measured in microteslas. Ignore z.