美文网首页codeTools
025-CoreLocation-定位管理器

025-CoreLocation-定位管理器

作者: ArrQing | 来源:发表于2017-03-06 09:31 被阅读2次
    #import <Foundation/Foundation.h>
    
    @protocol JFLocationDelegate <NSObject>
    
    /// 定位中
    - (void)locating;
    
    /**
     当前位置
    
     @param locationDictionary 位置信息字典
     */
    - (void)currentLocation:(NSDictionary *)locationDictionary;
    
    /**
     拒绝定位后回调的代理
    
     @param message 提示信息
     */
    - (void)refuseToUsePositioningSystem:(NSString *)message;
    
    /**
     定位失败回调的代理
    
     @param message 提示信息
     */
    - (void)locateFailure:(NSString *)message;
    
    @end
    
    @interface JFLocation : NSObject
    
    @property (nonatomic, strong) id<JFLocationDelegate> delegate;
    @end
    
    
    #import "JFLocation.h"
    
    #import <CoreLocation/CoreLocation.h>
    
    @interface JFLocation ()<CLLocationManagerDelegate>
    
    @property (nonatomic, strong) CLLocationManager *locationManager;
    @end
    
    @implementation JFLocation
    
    - (instancetype)init {
        if (self = [super init]) {
            [self startPositioningSystem];
        }
        return self;
    }
    
    - (void)startPositioningSystem {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [self.locationManager requestWhenInUseAuthorization];
        }
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = kCLDistanceFilterNone;
        [self.locationManager startUpdatingLocation];
    }
    
    #pragma mark CLLocationManagerDelegate
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            if (self.delegate && [self.delegate respondsToSelector:@selector(locating)]) {
                [self.delegate locating];
            }
        });
        CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
        [geoCoder reverseGeocodeLocation:[locations lastObject] completionHandler:^(NSArray *placemarks, NSError *error) {
            for (CLPlacemark * placemark in placemarks) {
                NSDictionary *location = [placemark addressDictionary];
                static dispatch_once_t onceToken;
                dispatch_once(&onceToken, ^{
                    if (self.delegate && [self.delegate respondsToSelector:@selector(currentLocation:)]) {
                        [self.delegate currentLocation:location];
                    }
                });
            }
        }];
        [manager stopUpdatingLocation];
    }
    
    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
        if ([error code] == kCLErrorDenied) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(refuseToUsePositioningSystem:)]) {
                [self.delegate refuseToUsePositioningSystem:@"已拒绝使用定位系统"];
            }
        }
        if ([error code] == kCLErrorLocationUnknown) {
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                if (self.delegate && [self.delegate respondsToSelector:@selector(locateFailure:)]) {
                    [self.delegate locateFailure:@"无法获取位置信息"];
                }
            });
        }
    }
    
    @end
    
    
    
    

    相关文章

      网友评论

        本文标题:025-CoreLocation-定位管理器

        本文链接:https://www.haomeiwen.com/subject/ovuygttx.html