Implementing a horizontal compass on the iPhone - algorithm?

A horizontal compass looks something like this if you are facing due East (90 degrees).


If you were facing due 355 degrees northwest, it would look like this:


As you turn the compass, the number should cycle from 0 -> 360 -> 0

So, my question is, how would you implement a view like this on the iPhone? I had a couple of ideas:

  1. Make one long image with all numbers and tick marks, and shift it left/right when the compass heading changes

  2. Create pieces of the view as tiles and append them when the compass heading changes.

  3. Create a line of tick marks that shifts with the compass heading, and just write numbers on it as needed.

How would you attack this problem? Im mainly looking for algorithmic advice, but if you ave code or pseudo-code to demonstrate, that would be helpful too.


Option one is the easiest. Keep in mind that you can composite part of an image to deal with the wrap-around.

Here is a complete solution of implementing horizontal compass in iPhone 4

if ([CLLocationManager locationServicesEnabled] && [CLLocationManager headingAvailable]) { m_locationManager=[[CLLocationManager alloc]init]; m_locationManager.desiredAccuracy=kCLLocationAccuracyBest; m_locationManager.headingOrientation=CLDeviceOrientationPortrait;; m_locationManager.delegate=self; [m_locationManager startUpdatingHeading];


pragma mark CLLocationManagerDelegate Methods

  • (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { CLLocationDirection direction= newHeading.magneticHeading; [m_readingsLabel setText:[NSString stringWithFormat:@"Degrees:%f",direction]];

    float radians=direction*M_PI/180; m_compassImageView.transform=CGAffineTransformMakeRotation(radians); }

You can build the modulo 10 of the heading and switch for 10, 5 and all others.

You just need to implement location manager functionality to find the exact direction in which your iPhone is pointing.

So, You can check this :

CLLocationManager *locationManager; [locationManager startUpdatingHeading];

And whenever you call startUpdatingHeading() it will call "- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading" method which you need to override from Location manager.

And that's it you can find the direction in which iPhone 3GS is pointing by newHeading.magneticHeading or newHeading.trueHeading.

You will surprise that you can get exact the same direction what magnetic compass gives you.

Need Your Help

Converting from a double loop to a single loop


This program builds a dictionary out of a list based on what two index numbers you would like from the list.

Delphi Interface Performance Issue

performance delphi oop interface

I have done some really serious refactoring of my text editor. Now there is much less code, and it is much easier to extend the component. I made rather heavy use of OO design, such as abstract cla...