20

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

In the CLPlacemark Reference from Apple it states:

administrativeArea The state or province associated with the placemark. (read-only) @property(nonatomic, readonly) NSString *administrativeArea Discussion If the placemark location is Apple’s headquarters, for example, the value for this property would be the string “CA” or “California”.

but whenever I use it, I only get the full state (i.e California) and not the abbreviation (i.e CA). Can anyone help me here?

sridvijay
  • 1,526
  • 18
  • 39
  • 1
    The [CLPlacemark-StateAbbreviation](https://github.com/jweyrich/CLPlacemark-StateAbbreviation) does exactly that. – jweyrich May 25 '15 at 18:53

7 Answers7

35

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
ChrisH
  • 4,468
  • 2
  • 33
  • 42
  • 1
    Thanks! I think this could be really useful for future uses/visitors who see this! – sridvijay Feb 02 '13 at 20:34
  • 1
    Thanks for accepting this so long after the original question...hopefully yeah it does come in useful to someone. – ChrisH Feb 05 '13 at 02:26
  • 2
    Hey, I turned this into a plist that also includes canadian provinces. A bit cleaner + you can add more to it (just load the dictionary from a file instead of writing it in code). This method is great though. http://cl.ly/3c0W1s3E1J1Y – shim Jul 19 '13 at 22:40
5

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

DLende
  • 5,162
  • 1
  • 17
  • 25
  • 1
    Thanks! But could you update the code to show how to implement this with the administrativeArea? Do I just put it in something like objectForKey or something? I'm a little confused on how to do that. Thanks for the answer! – sridvijay Jun 19 '12 at 21:12
  • Thanks for the update but I get 2 errors while doing this. 1. Instance variable 'strAbbreviation' accessed in class method. 2. Implicit conversion of NSUInteger to NSString * is disallowed in ARC. I took out the release statements to make it work in ARC, but I don't know how to solve these 2. Thanks agai. For helping, I appreciate it. – sridvijay Jun 19 '12 at 22:40
  • 1
    No problem, could you help me with the problem though :p? – sridvijay Jun 19 '12 at 22:55
2

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".

George Clingerman
  • 1,395
  • 9
  • 15
1

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.

xarux
  • 73
  • 8
1

For people who need the state list with objects and keys swapped (e.g. on iOS 7 I get "CA" from placemark.administrativeArea):

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

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
T Blank
  • 1,408
  • 1
  • 16
  • 21
  • 1
    This is not a true statement at least according to the most recent docs that Apple has available: The string in this property can be either the spelled out name of the administrative area or its designated abbreviation, if one exists. If the placemark location is Apple’s headquarters, for example, the value for this property would be the string “CA” or “California”. – Mark T. Jan 21 '16 at 19:43
0

SWIFT variant of dictionary

let states = [
    "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"
]