获取地理位置


#import 
@interface FuJinVC ()
@property (nonatomic , strong) CLLocationManager *manager;
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self location];
}
- (void)location{
    self.manager = [[CLLocationManager alloc] init];
    self.manager.delegate = self;
    [self.manager requestWhenInUseAuthorization];
    self.manager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;

    [self.manager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    DLog(@"失败%@",error);
     [manager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    NSLog(@"授权状态改变");
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    DLog(@"%@定位中",locations);
    CLLocation *new = [locations lastObject];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:new completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
        DLog(@"%@数组 %@错误",placemarks,error);
        if (placemarks.count > 0) {
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            NSString *city = placemark.administrativeArea;//获取城市
            NSString *country = placemark.country;// 获取国家
            DLog(@"位于:%@,%@",city,country);
            DLog(@"%@",placemark.addressDictionary[@"Name"]);

        }
    }];
    
    
    
    [manager stopUpdatingLocation];
}

你可能感兴趣的:(获取地理位置)