TZLocationManager.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // TZLocationManager.m
  3. // TZImagePickerController
  4. //
  5. // Created by 谭真 on 2017/06/03.
  6. // Copyright © 2017年 谭真. All rights reserved.
  7. // 定位管理类
  8. #import "TZLocationManager.h"
  9. #import "TZImagePickerController.h"
  10. @interface TZLocationManager ()<CLLocationManagerDelegate>
  11. @property (nonatomic, strong) CLLocationManager *locationManager;
  12. /// 定位成功的回调block
  13. @property (nonatomic, copy) void (^successBlock)(NSArray<CLLocation *> *);
  14. /// 编码成功的回调block
  15. @property (nonatomic, copy) void (^geocodeBlock)(NSArray *geocodeArray);
  16. /// 定位失败的回调block
  17. @property (nonatomic, copy) void (^failureBlock)(NSError *error);
  18. @end
  19. @implementation TZLocationManager
  20. + (instancetype)manager {
  21. static TZLocationManager *manager;
  22. static dispatch_once_t onceToken;
  23. dispatch_once(&onceToken, ^{
  24. manager = [[self alloc] init];
  25. manager.locationManager = [[CLLocationManager alloc] init];
  26. manager.locationManager.delegate = manager;
  27. [manager.locationManager requestWhenInUseAuthorization];
  28. });
  29. return manager;
  30. }
  31. - (void)startLocation {
  32. [self startLocationWithSuccessBlock:nil failureBlock:nil geocoderBlock:nil];
  33. }
  34. - (void)startLocationWithSuccessBlock:(void (^)(NSArray<CLLocation *> *))successBlock failureBlock:(void (^)(NSError *error))failureBlock {
  35. [self startLocationWithSuccessBlock:successBlock failureBlock:failureBlock geocoderBlock:nil];
  36. }
  37. - (void)startLocationWithGeocoderBlock:(void (^)(NSArray *geocoderArray))geocoderBlock {
  38. [self startLocationWithSuccessBlock:nil failureBlock:nil geocoderBlock:geocoderBlock];
  39. }
  40. - (void)startLocationWithSuccessBlock:(void (^)(NSArray<CLLocation *> *))successBlock failureBlock:(void (^)(NSError *error))failureBlock geocoderBlock:(void (^)(NSArray *geocoderArray))geocoderBlock {
  41. [self.locationManager startUpdatingLocation];
  42. _successBlock = successBlock;
  43. _geocodeBlock = geocoderBlock;
  44. _failureBlock = failureBlock;
  45. }
  46. #pragma mark - CLLocationManagerDelegate
  47. /// 地理位置发生改变时触发
  48. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
  49. [manager stopUpdatingLocation];
  50. if (_successBlock) {
  51. _successBlock(locations);
  52. }
  53. if (_geocodeBlock && locations.count) {
  54. CLGeocoder *geocoder = [[CLGeocoder alloc] init];
  55. [geocoder reverseGeocodeLocation:[locations firstObject] completionHandler:^(NSArray *array, NSError *error) {
  56. self->_geocodeBlock(array);
  57. }];
  58. }
  59. }
  60. /// 定位失败回调方法
  61. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
  62. NSLog(@"定位失败, 错误: %@",error);
  63. switch([error code]) {
  64. case kCLErrorDenied: { // 用户禁止了定位权限
  65. } break;
  66. default: break;
  67. }
  68. if (_failureBlock) {
  69. _failureBlock(error);
  70. }
  71. }
  72. @end