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

相關文章
相關標籤/搜索