12

Is it possible to use the native compass that iOS has within my own application? Or do I need to draw and animate my own compass?

iHunter
  • 6,135
  • 3
  • 35
  • 56
theDuncs
  • 4,211
  • 4
  • 35
  • 58

1 Answers1

30

There is no native compass UIView. In order to use the magnetometer, you'll have to use CoreLocation and the following delegate method:

- (void) locationManager:(CLLocationManager *)manager
             didUpdateHeading:(CLHeading *)newHeading

to rotate a UIView to point North (bearingView is a UIImageView):

float heading = newHeading.magneticHeading; //in degrees
float headingDegrees = (heading*M_PI/180); //assuming needle points to top of iphone. convert to radians
self.bearingView.transform = CGAffineTransformMakeRotation(headingDegrees);
Nur Iman Izam
  • 1,603
  • 1
  • 14
  • 12
Keller
  • 16,803
  • 8
  • 53
  • 71
  • Yes, I think you're right about the native compass UIView. I'll wait a little to see if anyone else disagrees. Certainly yes, didUpdateHeading is the way to update whatever image i choose to draw. Thanks so much. – theDuncs Feb 13 '12 at 18:07
  • 1
    Keller - since nobody else has contradicted you, I'll take your answer as the correct one. Thanks mate. – theDuncs Feb 20 '12 at 15:53
  • No problem. To create a compass view that points north, simply create a "compass needle" graphic and rotate it via it's transform. See my Edit. – Keller Feb 21 '12 at 01:25
  • 3
    Here is a full tutorial on how to implement this if anyone was wondering: http://blog.objectgraph.com/index.php/2012/01/10/how-to-create-a-compass-in-iphone/ – Albert Renshaw Dec 19 '12 at 06:04
  • @AlbertRenshaw http://blog.objectgraph.com/index.php/2012/01/10/how-to-create-a-compass-in-iphone/ link not working. Please help me to create compass in IOS objective-c – Dipanki Jadav Nov 17 '16 at 12:32
  • @DipankiJadav We are in luck! Archive.org has a copy of the site from before it went down! https://web.archive.org/web/20150421132328/http://blog.objectgraph.com/index.php/2012/01/10/how-to-create-a-compass-in-iphone – Albert Renshaw Nov 17 '16 at 17:52