@implementation LocationManager
{
CLLocationManager *_locaMgr;
}
DTSingletonM(LocationManager)
/**
请求用户定位权限,根据定位权限进行不同的操作
*/
- (void)requestLocation {
// 判断是否打开了系统的位置服务
if ([CLLocationManager locationServicesEnabled]) {
CLAuthorizationStatus locationStatus = [CLLocationManager authorizationStatus];
switch (locationStatus) {
case kCLAuthorizationStatusRestricted://无法控制,无法获知状态
{
}
break;
case kCLAuthorizationStatusNotDetermined://用户还未选择位置权限�
{
[self createLocaManagerUpdateNeedToPermission:YES];
}
break;
case kCLAuthorizationStatusAuthorizedAlways:
case kCLAuthorizationStatusAuthorizedWhenInUse://已经获取到权限,开启定位
{
[self createLocaManagerUpdateNeedToPermission:NO];
}
break;
case kCLAuthorizationStatusDenied://用户已经拒绝权限,去设置中开启权限
{
[self alertToSetting];
}
break;
default:
break;
}
return;
}
}
- (void)createLocaManagerUpdateNeedToPermission:(BOOL)needToPermission {
// 创建位置管理者对象
if (!_locaMgr) {
_locaMgr = [[CLLocationManager alloc] init];
}
if (needToPermission) {
[_locaMgr requestWhenInUseAuthorization];
}
_locaMgr.delegate = self; // 设置代理
// 设置定位距离过滤参数 (当本次定位和上次定位之间的距离大于或等于这个值时,调用代理方法)
_locaMgr.distanceFilter = 100;
_locaMgr.desiredAccuracy = kCLLocationAccuracyBest; // 设置定位精度(精度越高越耗电)
[_locaMgr startUpdatingLocation]; // 开始更新位置
}
- (void)alertToSetting {
//提示用户取打开位置
[LEEAlert alert].config.LeeTitle(@"提示").LeeContent(@"您没有开启定位服务").LeeCancelAction(@"取消", nil).LeeAddAction(^(LEEAction *action) {
action.title = @"去设置";
[action setClickBlock:^{
//点击事件
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}];
}).LeeShow();
}
/**
更新位置
@param manager manager
@param locations locations
*/
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
//位置移动
}
/**
开始定位
@param manager manager
@param region region
*/
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
//开始定位
}
/**
定位授权变化的回调
@param manager manager
@param status status
*/
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
//如果从不可定位变为可定位,那么就重新定位
if (status == kCLAuthorizationStatusAuthorizedAlways||
status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[self createLocaManagerUpdateNeedToPermission:NO];
}
}
网友评论