156 lines
6.1 KiB
Objective-C
156 lines
6.1 KiB
Objective-C
//
|
||
// QXLocationManager.m
|
||
// YSDTrucksProject
|
||
//
|
||
// Created by 党凯 on 2020/7/17.
|
||
// Copyright © 2020 党凯. All rights reserved.
|
||
//
|
||
|
||
#import "QXLocationManager.h"
|
||
|
||
|
||
@interface QXLocationManager()<CLLocationManagerDelegate>
|
||
@property (nonatomic,strong)CLLocationManager *lcManager;
|
||
@end
|
||
@implementation QXLocationManager
|
||
+(instancetype)shareManager{
|
||
static QXLocationManager *manager = nil;
|
||
static dispatch_once_t predicate;
|
||
dispatch_once(&predicate, ^{
|
||
manager = [[QXLocationManager alloc] init];
|
||
});
|
||
return manager;
|
||
}
|
||
- (instancetype)init
|
||
{
|
||
self = [super init];
|
||
if (self) {
|
||
// [AMapServices sharedServices].apiKey = mapKey;
|
||
// _locationManager = [[AMapLocationManager alloc] init];
|
||
// // 带逆地理信息的一次定位(返回坐标和地址信息)
|
||
// [_locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
|
||
// // 定位超时时间,最低2s,此处设置为2s
|
||
// _locationManager.locationTimeout =2;
|
||
// // 逆地理请求超时时间,最低2s,此处设置为2s
|
||
// _locationManager.reGeocodeTimeout = 2;
|
||
// _locationManager.delegate = self;
|
||
// if ([CLLocationManager authorizationStatus]) {
|
||
|
||
// 创建位置管理者对象
|
||
|
||
self.lcManager = [[CLLocationManager alloc] init];
|
||
|
||
self.lcManager.delegate = self; // 设置代理
|
||
|
||
// 设置定位距离过滤参数 (当本次定位和上次定位之间的距离大于或等于这个值时,调用代理方法)
|
||
|
||
self.lcManager.distanceFilter = 100;
|
||
|
||
self.lcManager.desiredAccuracy = kCLLocationAccuracyBest; // 设置定位精度(精度越高越耗电)
|
||
|
||
[self.lcManager requestWhenInUseAuthorization];//这句很关键!!!
|
||
|
||
// [self.lcManager startUpdatingLocation]; // 开始更新位置
|
||
|
||
// }else{
|
||
//
|
||
// //没开启,做其他提醒
|
||
//
|
||
// }
|
||
}
|
||
return self;
|
||
}
|
||
//-(void)amapLocationManager:(AMapLocationManager *)manager doRequireLocationAuth:(CLLocationManager *)locationManager{
|
||
// if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
|
||
// [locationManager requestAlwaysAuthorization];
|
||
// }
|
||
//}
|
||
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
|
||
NSLog(@"定位到了");
|
||
|
||
__weak typeof(self)weakSelf = self;
|
||
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
|
||
[geocoder reverseGeocodeLocation:[locations firstObject] completionHandler:^(NSArray<CLPlacemark *> *_Nullable placemarks, NSError * _Nullable error) {
|
||
|
||
CLPlacemark *place = [placemarks firstObject];
|
||
//place包含了地理信息
|
||
if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(locationSuccessWithCity:province:area:address:)]) {
|
||
/// 省
|
||
NSString *province = place.administrativeArea;
|
||
/// 市
|
||
NSString *city = place.locality;
|
||
/// 县
|
||
NSString *area = place.subLocality;
|
||
[weakSelf.delegate locationSuccessWithCity:place.locality province:province area:area address:place.name];
|
||
}
|
||
}];
|
||
}
|
||
|
||
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
|
||
NSLog(@"获取定位失败");
|
||
}
|
||
|
||
-(void)startLoction{
|
||
if (![self checkAuthorization]) {
|
||
UIAlertController *al = [UIAlertController alertControllerWithTitle:@"您没有打开定位权限" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
|
||
[al addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
|
||
|
||
}]];
|
||
[al addAction:[UIAlertAction actionWithTitle:@"去设置" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
|
||
//打开应用的设置界面
|
||
NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
|
||
if([[UIApplication sharedApplication] canOpenURL:url]) {
|
||
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
|
||
}
|
||
}]];
|
||
[KEYWINDOW.rootViewController presentViewController:al animated:YES completion:nil];
|
||
// [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
|
||
// resultBlock(location,regeocode,error);
|
||
// }];
|
||
[self.lcManager startUpdatingLocation];
|
||
}else{
|
||
// [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
|
||
// resultBlock(location,regeocode,error);
|
||
// }];
|
||
[self.lcManager startUpdatingLocation];
|
||
}
|
||
}
|
||
|
||
|
||
|
||
-(void)stopLoction{
|
||
[self.lcManager stopUpdatingLocation];
|
||
}
|
||
//检查定位权限
|
||
-(BOOL)checkAuthorization{
|
||
BOOL result = NO;
|
||
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
|
||
switch (status) {
|
||
//用户在设置中关闭定位功能,或者用户明确的在弹框之后选择禁止定位
|
||
case kCLAuthorizationStatusDenied:
|
||
result = NO;
|
||
break;
|
||
//用户没有选择是否要使用定位服务(弹框没选择,或者根本没有弹框)注:高德会自动请求权限
|
||
case kCLAuthorizationStatusNotDetermined:
|
||
result = YES;
|
||
break;
|
||
//定位服务授权状态受限制,可能由于活动限制了定位服务,并且用户不能改变当前的权限,这个状态有可能不是用户拒绝的,但是也有可能是用户拒绝的。
|
||
case kCLAuthorizationStatusRestricted:
|
||
result = NO;
|
||
break;
|
||
//App始终允许使用定位功能
|
||
case kCLAuthorizationStatusAuthorizedAlways:
|
||
result = YES;
|
||
break;
|
||
//用户在使用期间允许使用定位功能
|
||
case kCLAuthorizationStatusAuthorizedWhenInUse:
|
||
result = YES;
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
return result;
|
||
}
|
||
|
||
@end
|