iOS Location Service

@implementation IFLocationManager

@synthesize locationServiceFinished = _locationServiceFinished;
@synthesize locationServiceFailed = _locationServiceFailed;

- (id)init
{
    self = [super init];
    if (self) {
    }
    
    return self;
}

#pragma mark -
#pragma mark - 单例方法
+ (instancetype)manager
{
    static IFLocationManager *core = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        core = [IFLocationManager new];
    });
    return core;
}

#pragma mark -
#pragma mark - 启动定位服务
- (void)startLocation
{
    if (locationManager) {
        locationManager.delegate = nil;
        locationManager = nil;
    }
    
    if ([CLLocationManager locationServicesEnabled]) {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
        //不需要太高精度,费电
        locationManager.delegate = self;
  
        [locationManager startUpdatingLocation];
    }
}


#pragma mark -
#pragma mark - 停止定位服务
- (void)stopLocation
{
    [locationManager stopUpdatingLocation];
    locationManager = nil;
}

#pragma mark -
#pragma mark - 定位服务 - 更新地理位置回调方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation * newLocation = [locations lastObject];//最后一次位置是当前位置
    CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];
    CLGeocodeCompletionHandler handle = ^(NSArray *placemarks,NSError *error)
    {
        
        for (CLPlacemark *placeMark in placemarks)
        {
            NSDictionary *addressDic = placeMark.addressDictionary;
            __unused NSString *state = [addressDic objectForKey:@"State"];
            __unused NSString *city = [addressDic objectForKey:@"City"];
            __unused NSString *subLocality = [addressDic objectForKey:@"SubLocality"];
            __unused NSString *street = [addressDic objectForKey:@"Street"];
            __unused NSString *latitude = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];
            __unused NSString *longitude = [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude];
            
            if (_locationServiceFinished) {
                NSDictionary *values = [NSDictionary dictionaryWithObjectsAndKeys:
                                        state,LocationValue_State,
                                        city,LocationValue_City,
                                        subLocality,LocationValue_SubLocality,
                                        street,LocationValue_Street,
                                        latitude,LocationValue_Latitude,
                                        longitude,LocationValue_Longitude,
                                        placeMark.administrativeArea,LocationValue_AdministrativeArea,
                                        nil];
                
                _locationServiceFinished(values);
                
                UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"12345" message:@"s" delegate:nil cancelButtonTitle:@"c" otherButtonTitles:nil, nil];
                [a show];
            }
            
            [self stopLocation];
        }
    };
    
    [clGeoCoder reverseGeocodeLocation:newLocation completionHandler:handle];
}

#pragma mark -
#pragma mark - 定位服务 - 位置授权状态变化回调方法
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    switch (status) {
        
        case kCLAuthorizationStatusNotDetermined :
            if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
                if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
                    //iOS8新增 需要在info.plist中添加字段NSLocationAlwaysUsageDescription,值为空即可
                    [locationManager requestAlwaysAuthorization];
                }
                
                if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                    //iOS8新增 需要在info.plist中添加字段NSLocationWhenInUseUsageDescription,值为空即可
                    [locationManager requestWhenInUseAuthorization];
                }
            }
            
            break;
    
        case kCLAuthorizationStatusRestricted:
            NSLog(@"kCLAuthorizationStatusRestricted");
            break;
            
        case kCLAuthorizationStatusDenied :
            NSLog(@"kCLAuthorizationStatusDenied");
            break;
            
        case kCLAuthorizationStatusAuthorizedAlways:
            
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            
            break;
            
        default:

            break;
    }
}

#pragma mark -
#pragma mark - 定位服务 - 获取地理位置数据失败回调方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    
    if (error) {
        if (error.code==1){
            //不允许定位
            // 定位服务没有得到您的授权,默认定位北京!
            if(_locationServiceFailed){
                _locationServiceFailed(LocationServiceError_NOAuthorize);
            }
        }else{
            //定位失败,默认定位北京!
            if(_locationServiceFailed){
                _locationServiceFailed(LocationServiceError_LocationError);
            }
            
        }
    }
    
    [self stopLocation];
    
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值