Files
featherVoice/QXLive/Manager/QXLocationManager.m

156 lines
6.1 KiB
Mathematica
Raw Normal View History

2025-08-08 10:49:36 +08:00
//
// 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];
// // 2s2s
// _locationManager.locationTimeout =2;
// // 2s2s
// _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