• iOS8更新后定位问题CLLcationManage


    在IOS8更新以后以前的方法CLLocationManagerDelegate不调用didUpdateLocations

    iOS8修改了位置设置里的内容,增加了一套状态(使用中可用/通常可用),所以以前的CLLcationManage的注册后, 
    Delegate接口不响应了,研究了一上午终于可以用了

    说一下我的心得

    (1)添加corelocation.framework

    (2)在Info.plist中加入两个缺省没有的字段

    • NSLocationAlwaysUsageDescription

    • NSLocationWhenInUseUsageDescription

    (3)导入头文件

    #import <CoreLocation/CoreLocation.h>

    #import <CoreLocation/CLLocationManagerDelegate.h>

    @interface PersonViewController : UIViewController<CLLocationManagerDelegate>{
        CLLocationManager* locationManager;
    }
    
    @property (strong, nonatomic) CLLocationManager* locationManager;
    
    @end

    (4)实例化并完成代理方法

    -(void)startLocation{
        [self.locationManager requestAlwaysAuthorization];
        if ([CLLocationManager locationServicesEnabled]&&[CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) {
            self.locationManager = [[CLLocationManager alloc] init];
            [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
            self.locationManager.delegate = self;
            if (__IPHONE_8_0) {
                NSLog(@"here");
                self.locationManager.distanceFilter = kCLDistanceFilterNone;
                [self.locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
            }
            NSLog(@"that's here");
                [self.locationManager startUpdatingLocation];
        }
    }
    
    - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
        switch (status) {
            case kCLAuthorizationStatusNotDetermined:
            if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
                [locationManager requestWhenInUseAuthorization];            }            break;
            default:
            break;
        }
    }
    
    
    
    //定位代理经纬度回调
    
    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
        [locationManager stopUpdatingLocation];
        NSLog(@"location ok");
        CLLocation *currentLocation = [locations firstObject];
        NSLog(@"%@",[NSString stringWithFormat:@"经度:%3.5f
    纬度:%3.5f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude]);
        CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
        [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
            for (CLPlacemark * placemark in placemarks) {
                
                NSDictionary *test = [placemark addressDictionary];
                //  Country(国家)  State(城市)  SubLocality(区)
                NSLog(@"%@", [test objectForKey:@"State"]);
                _locationCity.text = [test objectForKey:@"SubLocality"];
            }
        }];
    
    }
    
    - (void)locationManager:(CLLocationManager *)manager
           didFailWithError:(NSError *)error {
        
        if (error.code == kCLErrorDenied) {
            NSLog(@"location%ld",(long)error.code);
        }
    }

    (5)设置开始定位结束定位

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        [self startLocation]; // 开始定位
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
        [locationManager stopUpdatingLocation]; // 停止定位
    }

    如果还不能定位的欢迎留言交流!

  • 相关阅读:
    排列数组所有情况
    查到的结果的某个字段在一串字符串之中
    element组件化跳转和路由式跳转
    vue路由and组件操作
    事件 绑定,取消冒泡,拖拽 ,点击,事件委托习题
    窗口属性 和DOM 元素尺寸位置 及习题加强
    DOM树的增删改查 和 Date定时任务
    JS DOM 初做了解,习题笔记
    struts配置及检验
    第一个JSP登录跳转
  • 原文地址:https://www.cnblogs.com/csdnIOS/p/4203057.html
Copyright © 2020-2023  润新知