CLPlacemark – 状态缩写?
我想知道是否有可能从CLPlacemark得到州的缩写?
在苹果的CLPlacemark Reference中指出:
administrativeArea与地标关联的州或省。 (只读)@property(非primefaces,只读)NSString * administrativeArea讨论如果地标位置是苹果的总部,例如,这个属性的值将是string“CA”或“California”。
但每当我使用它,我只得到完整的国家(即加利福尼亚州),而不是缩写(即CA)。 有人能帮我一下吗?
对于需要此解决scheme的其他人,我已经为CLPlacemark创build了一个类别类,它返回短状态string。 所有你需要做的就是调用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
我认为你不能得到国家的缩写,但你可以让你自己的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
当你给class级打电话时,应该是这样的
NSString *upperCase = [@"California" uppercaseString]; // California could be from (NSString *)placemark.administrativeArea; NSString *abbr = [StateAbbreviation allStates:upperCase]; NSLog(@"%@", abbr); // Result should be CA
这只是你可以研究所有状态的样本, 状态和它们的缩写也是这样的状态和它们的缩写
我相信这个文档是不正确的。 行政区总是要返回美国地方的全名。 要获得州名缩写,您最有可能必须创build一个字典查找表,以便search关键字“California”将返回值“CA”。
这是使用FormattedAddressLines的另一个类别,它返回一个结果,如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; }
不完美,但只要苹果改变我认为的地址线的格式,它就可以工作。
对于需要交换对象和键的状态列表的人(例如在iOS 7上,我从placemark.administrativeArea
获得“CA”):
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];
至lessiOS 8, CLPlacemark
的administrativeArea
返回一个两个字母的美国国家的缩写。
只要你的目标是iOS 8和更新版本(你现在应该这样做),你就不需要在CLPlacemark中扩展一个类似于被接受的答案的类别。
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); }];
运行这个,你会得到:
State: CA