iOS8 CLLocationManager 、CLGeocoder获取地理位置

最近在ios8.0使用CLLocationManager定位服务,发现老不能定位,查看设置菜单中的项也是处于未知状态.想起以前都有一个弹出框提示用户是否容许定位,此次一直没有出现了.原来ios8.0下的定位服务须要申请受权了. 具体代码以下:html

 

 if ([CLLocationManager locationServicesEnabled]) {ios

  self.locationManager = [[CLLocationManager alloc] init];git

 _locationManager.delegate = self;spa

 _locationManager.desiredAccuracy = kCLLocationAccuracyBest; //控制定位精度,越高耗电量越大。code

 _locationManager.distanceFilter = 100; //控制定位服务更新频率。单位是“米”orm

  [_locationManager startUpdatingLocation];htm

   //在ios 8.0下要受权blog

 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)ip

 [_locationManager requestWhenInUseAuthorization];  //调用了这句,就会弹出容许框了.string

 }

 

注意:

   在Info.plist文件还要加上NSLocationWhenInUseUsageDescription这个key,Value能够为空,

 

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

  

    CLLocation * currLocation = [locations lastObject];

 

    NSLog(@"%@",[NSString stringWithFormat:@"%.3f",currLocation.coordinate.latitude]);

    NSLog(@"%@",[NSString stringWithFormat:@"%.3f",currLocation.coordinate.longitude]);

  //解析 获取地理位置

 if (![Mycoder isValid]) {

        Mycoder=[[CLGeocoder alloc]init];

    }

    [Mycoder reverseGeocodeLocation:currLocation completionHandler:^(NSArray *placemarks, NSError *error) {

        if ([placemarks count]>0) {

            CLPlacemark *place=[placemarks objectAtIndex:0];

            NSDictionary *dict=place.addressDictionary;

            NSLog(@"%@\n%@\n%@\n",[dict safeStringForKey:@"City"],[dict safeStringForKey:@"SubLocality"],[dict safeStringForKey:@"Street"]);

            [bt_local setTitle:[dict safeStringForKey:@"Name"] forState:UIControlStateNormal];           

        }

    }];

 

}

使用CLGeocoder获取地址位置,CLPlacemark的具体属性,请看连接:http://www.cnblogs.com/niit-soft-518/p/4052366.html

相关文章
相关标签/搜索