Skip to content

Instantly share code, notes, and snippets.

@jimjeffers
Last active August 29, 2015 13:56
Show Gist options
  • Save jimjeffers/8868901 to your computer and use it in GitHub Desktop.
Save jimjeffers/8868901 to your computer and use it in GitHub Desktop.
Get the expected reading from the iPhone's compass when holding the phone in either landscape orientation.
- (void)locationManager:(CLLocationManager*)manager
didUpdateHeading:(CLHeading*)newHeading {
// If the accuracy is valid, process the event.
if (newHeading.headingAccuracy > 0) {
/**
* The orientation affects the compass. We need to actually
* base the orientation off of the top of the iPhone. Think
* of the direction the phone is facing when you hold it right
* side up in portrait mode. That is the reading the compass
* actually gives us. So in our situation we need to actually
* offset by 90 degrees. When the phone is facing north (0) but
* you orientate in landscape the phone will think it is
* actually facing east (90) or west (180) depending on which landscape
* orientation you end up using.
*/
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
float offsetDirection = newHeading.magneticHeading-90.f;
if (offsetDirection < 0.f) {
offsetDirection += 360.f;
}
self.direction = offsetDirection;
} else if (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
float offsetDirection = newHeading.magneticHeading+90.f;
if (offsetDirection > 360.f) {
offsetDirection -= 360.f;
}
self.direction = offsetDirection;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment