美文网首页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-定位管理器

  • iOS定位服务

    1、导入框架 3、导入主头文件 4、声明管理器和代理 5、初始化管理器 6、开启定位服务,需要定位时调用findM...

  • 20170707 AWT界面编程(下)

    参考文献:《Java疯狂讲义》(第三版) 绝对定位 绝对定位步骤: 1、Container的布局管理器设成null...

  • 地图定位

    定位使用 " CoreLocation 框架 想要定位,需要使用5个步骤 1.首先创建一个"强引用"的位置管理器C...

  • LBS地图定位

    定位使用 " CoreLocation 框架想要定位,需要使用5个步骤 1.首先创建一个"强引用"的位置管理器C...

  • 荣耀70强制安装第三方桌面的方法

    打开MT管理器[https://mt2.cn],定位到APK 点击 APK,点击查看 点击AndroidManif...

  • 10个Eclipse珍藏插件推荐

    ​ 1、Open Explorer 打开资源管理器插件,这是一个从Eclipse里面可以直接定位打开windows...

  • iOS 地图定位-定位

    定位 常用方法的介绍 CLLocationManager位置管理器,我们的有关于位置的方法和属性都是通过它来管理设...

  • 14.5-全栈Java笔记:java.awt这些布局怎么写?|流

    布局管理器 读者会发现,如果使用坐标定位法(空布局),在一个比较复杂的界面上定位每个控件的坐标是一个非常麻烦的工作...

  • 从源头消灭弹窗——定位

    从源头消灭弹窗——定位 Process Explorer 介绍 这款免费软件是微软家出品的,它是一款任务管理器软件...

网友评论

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

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