#import "CoreLocationViewController.h"
//引入头文件
#import <CoreLocation/CoreLocation.h>
@interface CoreLocationViewController ()<CLLocationManagerDelegate>//遵循协议
/* 声明位置管理器属性 */
@property (strong,nonatomic) CLLocationManager *locationManager;
@end
@implementation CoreLocationViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
//判断你是否打开定位服务
if (![CLLocationManager locationServicesEnabled]) {
NSLog(@"定位服务当前尚未打开,请设置打开");
return;
}
//如果没有授权定位服务,则请求用户授权
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
/** 在info.plist中添加如下字段中的任意一个
* ①NSLocationWhenInUseUsageDescription YES
* ②NSLocationAlwaysUsageDescription YES
* 如果两个同时添加,则默认为第①个,但是如果只添加了第②个的话
*/
[self.locationManager requestAlwaysAuthorization];
}
//如果被授权
else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways){
//设置代理
self.locationManager.delegate = self;
/** 定位精度:枚举类型
* kCLLocationAccuracyBest;最精确的
* kCLLocationAccuracyNearestTenMeters;十米误差范围
* kCLLocationAccuracyHundredMeters;百米误差范围
* kCLLocationAccuracyKilometer;千米误差范围
* kCLLocationAccuracyThreeKilometers;三千米
*/
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//定位距离(每隔多少米定位一次,单位:米)
CLLocationDistance distance = 10.0;//每隔多少米定位一次
self.locationManager.distanceFilter = distance;
//启动定位服务
[self.locationManager startUpdatingLocation];
}
}
@end
网友评论