CLPlacemark - State Abbreviations?

后端 未结 7 1499
隐瞒了意图╮
隐瞒了意图╮ 2021-02-19 00:24

I was wondering if it was possible to get the state abbreviations from CLPlacemark?

In the CLPlacemark Reference from Apple it states:

administrativeArea The sta

7条回答
  •  清歌不尽
    2021-02-19 00:47

    As of at least iOS 8, CLPlacemark's administrativeArea returns a two-letter abbreviation for US States.

    You don't need to extend CLPlacemark with a category like the one in the accepted answer as long as you're targeting iOS 8 and newer (which you should be by now).

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:@"1 Infinite Loop, Cupertino, CA" completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark = [placemarks firstObject];
        NSLog(@"State: %@", placemark.administrativeArea);
    }];
    

    Run this and you'll get:

    State: CA
    

提交回复
热议问题