#import <CoreLocation/CoreLocation.h>
- (void)viewDidLoad {
[super viewDidLoad];
CLLocationManager * _manger = [[CLLocationManager alloc]init];
//从IOS8(Xcode6)开始,定位权限必须主动申请,首先在info.plist 中添加
//NSLocation AlwayUsageDescription
//NSLocationWhenInUseUsageDescription
//然后调用此方法
// [_manger requestWhenInUseAuthorization];
// [_manger requestAlwaysAuthorization]
[_manger requestWhenInUseAuthorization];
_manger.delegate = self;
//判断设备是否支持方向
if ([CLLocationManager headingAvailable]) {
NSLog(@"设备支持方向识别");
//设置监视精确度,当方向改变量达到多少才调用
_manger.headingFilter = 1;
//开启方向监视,当设备方向发生变化时,就会调用方向更新的代理方法
[_manger startUpdatingHeading];
}else{
NSLog(@"不支持方向识别");
}
//判断设备的定位功能是否可用
if ([CLLocationManager locationServicesEnabled]) {
NSLog(@"定位可用");
//设置调用位置改变方法的最小移动距离
_manger.distanceFilter = kCLDistanceFilterNone;
//设置定位的精确度,最小可以精确到来
_manger.desiredAccuracy= kCLLocationAccuracyNearestTenMeters;
//如果定位可用开启位置监视
[_manger startUpdatingLocation];
}else{
NSLog(@"定位不可用");
}
//创建一个坐标点
CLLocationCoordinate2D corrdinate = CLLocationCoordinate2DMake(12.454, -12.454);
//根据坐标点和半径生成一个圆形区域
CLCircularRegion * region = [[CLCircularRegion alloc]initWithCenter:corrdinate radius:50 identifier:@"起个名字叫region"];
//开启区域监视
[ _manger startMonitoringForRegion:region];
}
//当设备进入指定范围调用此方法
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
}
//当设备离开某个区域时调用
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{
}
//当设备方向发生变化时调用
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
//北是0.东90 南180西270
NSLog(@"磁力计方向%F",newHeading.magneticHeading);
NSLog(@"真实方向%F",newHeading.trueHeading);
}
//当设备位置发生变化是调用
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
NSLog(@"%@",locations);
//NSLocationAlwaysUsageDescription
//NSLocationWhenInUseUsageDescription
//表示一个定位信息
CLLocation * location = [locations lastObject];
//表示这次定位的时间
NSDate * date = location.timestamp;
//判断定位的时间是否距离现在过长
double time = [date timeIntervalSinceNow];
//
if (abs(time) < 10) {
NSLog(@"纬度%f",location.coordinate.latitude);
NSLog(@"经度%f",location.coordinate.longitude);
}
}