问题
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!
回答1:
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.
回答2:
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; } ... }
来源:https://stackoverflow.com/questions/2518381/iphone-mkreversegeocoder-adminstrativearea-getting-state-abbreviation