iPhone — MKReverseGeocoder.adminstrativeArea — get

2019-07-10 03:38发布

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!

2条回答
Fickle 薄情
2楼-- · 2019-07-10 04:21

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.

查看更多
Lonely孤独者°
3楼-- · 2019-07-10 04:21

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;
    }    
...
}
查看更多
登录 后发表回答