Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone -- MKReverseGeocoder.adminstrativeArea -- getting state abbreviation

In the documentation for MKReverseGeocoder, the administrativeArea property gives you the current state the user is in, and it mentions in an example that it returns EITHER the state name OR its abbreviation. I am wondering if anyone knows how to get the abbreviation instead of the full state name...I have been able to find nothing that shows this is even a possibility besides that brief example that doesn't mention HOW.

Thanks!

like image 609
RayJ Avatar asked Aug 31 '26 17:08

RayJ


2 Answers

I also needed to convert the State field from MKReverseGeocoder into a two letter abbreviation, so I created this plist:

https://github.com/djibouti33/US-State-Abbreviations

Here's how I use it:

// in my init
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"USStateAbbreviations" ofType:@"plist"];
self.usStateAbbreviations = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

// MKReverseGeocoder delegate method
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {   
... 
    NSString *state = [address objectForKey:@"State"];
    NSString *stateAbbreviation = [self.usStateAbbreviations objectForKey:[state uppercaseString]];
    NSString *stateTarget = state;
    if (stateAbbreviation) {
        stateTarget = stateAbbreviation;
    }    
...
}
like image 138
djibouti33 Avatar answered Sep 02 '26 08:09

djibouti33


There is no way to do this. Not sure why the Apple docs say "CA or California".

It's easy to convert state to 2 letter name. Just create a plist (table, or NSDictionary works too) of the following: http://www.usps.com/ncsc/lookups/usps_abbreviations.html and use that to look up the 2 letter abbreviations.

like image 38
Felix Khazin Avatar answered Sep 02 '26 08:09

Felix Khazin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!