iPhone — MKReverseGeocoder.adminstrativeArea — getting state abbreviation

那年仲夏 提交于 2019-12-24 00:12:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!