015-首页 模拟器定位

1 新建一个类 声明 代理
2 实现 在 想要 定位的 c 中 来实现 用到 协议 建立 类的 属性

import

@protocol JFLocationDelegate

/// 定位中

  • (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 delegate;
@end

2 实现 定位

import "JFLocation.h"

import

@interface JFLocation ()

@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 *)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

c 中 要实现的 对应的 协议方法

pragma mark --- JFLocationDelegate 开机定位

//定位中...

  • (void)locating {
    NSLog(@"定位中...");
    }

//定位成功

  • (void)currentLocation:(NSDictionary *)locationDictionary {
    NSString *city = [locationDictionary valueForKey:@"City"];
if (![_leftButton.titleLabel.text isEqualToString:city]) {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"您定位到%@,确定切换城市吗?",city] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [_leftButton setTitle:city forState:UIControlStateNormal];
        [KCURRENTCITYINFODEFAULTS setObject:city forKey:@"locationCity"];
        [KCURRENTCITYINFODEFAULTS setObject:city forKey:@"currentCity"];
        [self.manager cityNumberWithCity:city cityNumber:^(NSString *cityNumber) {
            [KCURRENTCITYINFODEFAULTS setObject:cityNumber forKey:@"cityNumber"];
        }];
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

}

/// 拒绝定位

  • (void)refuseToUsePositioningSystem:(NSString *)message {
    NSLog(@"%@",message);
    JFLog(@"%@", message);
    }

/// 定位失败

  • (void)locateFailure:(NSString *)message {
    NSLog(@"%@",message);
    }

你可能感兴趣的:(015-首页 模拟器定位)