问题描述
我想知道是否可以从CLPlacemark获取州缩写?
I was wondering if it was possible to get the state abbreviations from CLPlacemark?
在Apple的CLPlacemark参考中,它声明:
In the CLPlacemark Reference from Apple it states:
administrativeArea
与地标关联的州或省。 (只读)
@property(非原子,只读)NSString * administrativeArea
讨论
例如,如果地标位置是Apple的总部,则此属性的值将为字符串CA 或加利福尼亚。
administrativeAreaThe state or province associated with the placemark. (read-only)@property(nonatomic, readonly) NSString *administrativeAreaDiscussionIf the placemark location is Apple’s headquarters, for example, the value for this property would be the string "CA" or "California".
但每当我使用它时,我只获得完整状态(即加利福尼亚州)而不是缩写(即CA)。任何人都可以帮助我吗?
but whenever I use it, I only get the full state (i.e California) and not the abbreviation (i.e CA). Can anyone help me here?
推荐答案
对于其他需要解决方案的人,我已经为CLPlacemark创建了一个类别类返回短状态字符串。您需要做的就是致电 myPlacemark shortState
For anyone else that needs a solution for this, I've created a category class for CLPlacemark that returns the short state string. All you need to do is call myPlacemark shortState
CLPlacemark + ShortState.h
CLPlacemark+ShortState.h
#import <CoreLocation/CoreLocation.h>
#import <Foundation/Foundation.h>
@interface CLPlacemark (ShortState)
- (NSString *)shortState;
@end
CLPlacemark + ShortState.m
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
这篇关于CLPlacemark - 状态缩写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!