CLPlacemark - State Abbreviations?

I think you can't get the abbreviations of the states but you can make your own class for this..

  • List all the states(states are standards)
  • compare those states and return the abbreviation

Code..

Class StateAbbreviation

StateAbbreviation.h

@interface StateAbbreviation : NSString {

}

+ (NSString *)allStates:(int)index;
+ (NSString *)abbreviatedState:(NSString *)strState;

@end

StateAbbreviation.m

@implementation StateAbbreviation
+ (NSString *)allStates:(NSString *)strState {
   // Remove all space on the string
   strState = [strState stringByReplacingOccurrencesOfString:@" " withString:@""];
   //Sample states
   NSArray *states = [[NSArray alloc] initWithObjects:
                       @"ALABAMA",                      
                       @"ALASKA",        //AK
                       @"AMERICANSAMOA", //AS
                       @"ARIZONA",       //AZ
                       @"ARKANSAS",      //AR
                       @"CALIFORNIA",    //CA
                       nil];
  NSUInteger n = [states indexOfObject:strState];
  if (n > [states count] - 1) {
     strAbbreviation = @"NOSTATE";
  }
  else {
     strAbbreviation =[self abbreviatedState:n];
  }
  [states release];
  return strAbbreviation;
}

+ (NSString *)abbreviatedState:(int)index {
    NSArray *states = [[NSArray alloc] initWithObjects:
                       @"AL",
                       @"AK",
                       @"AS",
                       @"AZ",
                       @"AR",
                       @"CA",
                       nil];
       NSString *strAbbreviation = [states objectAtIndex:index];
       [states release];
       return strAbbreviation;
}
@end

When you call the class it should be something like this

NSString *upperCase = [@"California" uppercaseString]; // California could be from (NSString *)placemark.administrativeArea;
NSString *abbr = [StateAbbreviation allStates:upperCase];
NSLog(@"%@", abbr); // Result should be CA

This are only samples you can research all states something like this, states and their abbreviations also like this states and their abbreviations


I believe the documentation is just incorrect. The administrativeArea is always going to return the full state name for places in the United States. To get the state abbreviation you'll most likely have to create a dictionary look up table so that searching for the key "California" will return you the value "CA".


Here is another category using FormattedAddressLines, it returns a result like California, CA

-(NSString *) stateWithAbbreviation {
if ([[self.addressDictionary objectForKey:@"CountryCode"] isEqualToString:@"US"] && self.addressDictionary) {
    NSDictionary *addressLines = [self.addressDictionary objectForKey:@"FormattedAddressLines"];
    for (NSString* addressLine in addressLines) {
        NSRange stateRange = [addressLine rangeOfString:self.postalCode options:NSCaseInsensitiveSearch];
        if (stateRange.length > 0) {
            NSRange lastSpace = [addressLine rangeOfString:@" " options:NSBackwardsSearch];
            if (lastSpace.length > 0) {
                NSString *state = [[addressLine substringToIndex:lastSpace.location] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
                lastSpace = [state rangeOfString:@" " options:NSBackwardsSearch];
                if (lastSpace.length > 0) {
                    NSString *abbr =  [[state substringFromIndex:lastSpace.location] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
                    return  [NSString stringWithFormat:@"%@, %@", self.administrativeArea, abbr];
                }
            }
        }
    }
}
return self.administrativeArea;
}

Not perfect but it works as long as Apple changes the format of the address lines I think.


For anyone else that needs a solution for this, I've created a category class for CLPlacemark that returns the short state string. All you need to do is call myPlacemark shortState

CLPlacemark+ShortState.h

#import <CoreLocation/CoreLocation.h>
#import <Foundation/Foundation.h>

@interface CLPlacemark (ShortState)

- (NSString *)shortState;

@end

CLPlacemark+ShortState.m

#import "CLPlacemark+ShortState.h"

@interface CLPlacemark (ShortStatePrivate)

- (NSDictionary *)nameAbbreviations;

@end

@implementation CLPlacemark (ShortState)

- (NSString *)shortState {

    NSString *state = [self.administrativeArea lowercaseString];

    if (state.length==0)
        return nil;

    return [[self nameAbbreviations] objectForKey:state];

}

- (NSDictionary *)nameAbbreviations {

    static NSDictionary *nameAbbreviations = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        nameAbbreviations = [NSDictionary dictionaryWithObjectsAndKeys:
                             @"AL",@"alabama",
                             @"AK",@"alaska",
                             @"AZ",@"arizona",
                             @"AR",@"arkansas",
                             @"CA",@"california",
                             @"CO",@"colorado",
                             @"CT",@"connecticut",
                             @"DE",@"delaware",
                             @"DC",@"district of columbia",
                             @"FL",@"florida",
                             @"GA",@"georgia",
                             @"HI",@"hawaii",
                             @"ID",@"idaho",
                             @"IL",@"illinois",
                             @"IN",@"indiana",
                             @"IA",@"iowa",
                             @"KS",@"kansas",
                             @"KY",@"kentucky",
                             @"LA",@"louisiana",
                             @"ME",@"maine",
                             @"MD",@"maryland",
                             @"MA",@"massachusetts",
                             @"MI",@"michigan",
                             @"MN",@"minnesota",
                             @"MS",@"mississippi",
                             @"MO",@"missouri",
                             @"MT",@"montana",
                             @"NE",@"nebraska",
                             @"NV",@"nevada",
                             @"NH",@"new hampshire",
                             @"NJ",@"new jersey",
                             @"NM",@"new mexico",
                             @"NY",@"new york",
                             @"NC",@"north carolina",
                             @"ND",@"north dakota",
                             @"OH",@"ohio",
                             @"OK",@"oklahoma",
                             @"OR",@"oregon",
                             @"PA",@"pennsylvania",
                             @"RI",@"rhode island",
                             @"SC",@"south carolina",
                             @"SD",@"south dakota",
                             @"TN",@"tennessee",
                             @"TX",@"texas",
                             @"UT",@"utah",
                             @"VT",@"vermont",
                             @"VA",@"virginia",
                             @"WA",@"washington",
                             @"WV",@"west virginia",
                             @"WI",@"wisconsin",
                             @"WY",@"wyoming",
                             nil];
    });

    return nameAbbreviations;
}

@end