2

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)?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
dbarrett
  • 219
  • 2
  • 14

1 Answers1

2

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;
}
chrisbtoo
  • 1,572
  • 9
  • 13
  • I was hoping to avoid all that, but I guess I have to. Thank you. – dbarrett Dec 15 '09 at 14:58
  • Hmm. I wonder if this can be sanely handled for provinces as well? (Accents may or may not be passed back.) I seem to recall reading that there was a way to do a comparison that lifted all of those differences away. – Joe D'Andrea Mar 01 '12 at 19:07
  • You might also check out this question http://stackoverflow.com/questions/2518381/iphone-mkreversegeocoder-adminstrativearea-getting-state-abbreviation for a reference to a pre-built plist file containing the mapping and sample code to extract it. – Norman H Mar 14 '13 at 10:30