1,添加框架 CoreLocation.framework
2,在ViewController.h中写如下代码
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
CLLocationManager * _locationManger;
}
@property (retain, nonatomic) IBOutlet UILabel *header;
@end
3,ViewController.m代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_locationManger = [[CLLocationManager alloc]init];
if([CLLocationManager headingAvailable]){
_locationManger.headingFilter = 0.1 ;//设置精度,当度数改变0.1度后通知
[_locationManger startUpdatingHeading];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
if(newHeading.headingAccuracy>0){
CLLocationDirection theHeding = newHeading.magneticHeading;
_header.text = [NSString stringWithFormat:@"%lf度",theHeding];
}
}