objective-ccllocationmkplacemark

MKPlacemark: return US state abbreviation in AdministrativeArea


I'm using the MKPlacemark class to populate a label with location specifics. When calling the AdministrativeArea property, the entire name of the US state is returned (e.g. West Virginia). Is there a way to return ONLY the initials (e.g. WV)?


Solution

  • Apple's docs for that property suggest that there's no real definition for what it can contain. Your best bet is probably to create a function to map from the full state name to the 2 letter code, and pass the result of the property through it before display. I would default to the original string if you don't get a match.

    -(NSString *)codeFromState:(NSString *)state {
      NSArray *map = [NSArray arrayWithObjects:@"Alabama",@"AL", @"Alaska",@"AK", ... @"Wyoming", @"WY", nil];
      for (int i = 0; i <[map count]; i+=2) {
        if ([state compare:[map objectAtIndex:i]] == NSOrderedSame) {
          return [map objectAtIndex:i+1];
        }
      }
      return state;
    }