Posted on by & filed under Content - Highlights and Reviews, Mobile Development.

Most Augmented Reality (AR) apps show a compass that displays the direction you’re heading in.  This may seem like a complicated thing to do, but it’s actually quite simple.  We previously showed how to retrieve your magnetic heading from the iPhone’s compass using the CoreLocation framework in iOS 5.  By using this heading information, we can rotate any kind of view to point to magnetic north.

First, we’ll need an image to rotate (see below).

Once you have an image of a compass needle that is pointing up, you can use it to accurately represent magnetic north.

To keep it simple, let’s suppose that you have an instance variable of a UIImageView called _needle with it’s getter and setter generated by using the following:

We can then setup the UIImageView to use an image named “needle.png” that is 70px wide by 70px high.

[self setNeedle:needle];
[needle release];

Within the locationManager:didUpdateHeading: delegate method, we can take the magnetic heading and use it to apply a transform on the UIImageView.

What we’re doing here is first taking the magnetic heading of the compass.  We’re then converting it to radians, and finally setting the transform using the CGAffineTransformMakeRotation function.  If you now run your iOS 5 application, you’ll see the needle rotating and pointing to magnetic north while you rotate the phone as shown in this figure:

Safari Books Online has the content you need

Take advantage of these Augmented Reality and iOS resources in Safari Books Online:

After reading Pro iOS 5 Augmented Reality, you’ll be able to build iOS 5 augmented reality rich media apps or integrate all the best augmented reality techniques and tools into your existing iOS 5 apps.
iOS 5 Essentials will help you learn how to build simple, yet powerful iOS 5 applications incorporating iCloud Storage, Twitter, Core Image and Newsstand integration.
In The iOS 5 Developer’s Cookbook: Core Concepts and Essential Recipes for iOS Programmers, Third Edition, bestselling author and iOS development guru Erica Sadun brings together all the information you need to quickly start building successful iOS 5 apps for iPhone, iPad, and iPod touch.

About the Author

  Brendan Lim is an accomplished Ruby, iOS, Android, and Mac developer (learn more about him at He has spoken and keynoted at various conferences on cutting-edge tech and mobile platforms. He’s a Y Combinator alum and Co-founder of Kicksend (, an extremely simple way to instantly send files to the people you know. Brendan is also the co-author of MacRuby in Action.

Tags: Augmented Reality, CoreLocation, iOS 5,

Comments are closed.