19

我想知道是否可以从 CLPlacemark 获得州缩写?

在 Apple 的 CLPlacemark Reference 中,它指出:

行政区域 与地标相关的州或省。(只读)@property(nonatomic, readonly) NSString *administrativeArea 讨论 例如,如果地标位置是 Apple 的总部,则此属性的值将是字符串“CA”或“California”。

但每当我使用它时,我只会得到完整的州(即加利福尼亚)而不是缩写(即 CA)。有人能帮我一下吗?

4

7 回答 7

35

对于其他需要解决方案的人,我为 CLPlacemark 创建了一个类别类,它返回短状态字符串。您需要做的就是打电话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
于 2013-02-01T19:39:56.487 回答
6

我认为您无法获得各州的缩写,但您可以为此创建自己的课程..

  • 列出所有状态(状态是标准)
  • 比较这些状态并返回缩写

代码..

类状态缩写

状态缩写.h

@interface StateAbbreviation : NSString {

}

+ (NSString *)allStates:(int)index;
+ (NSString *)abbreviatedState:(NSString *)strState;

@end

州缩写.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

当你打电话给班级时,它应该是这样的

NSString *upperCase = [@"California" uppercaseString]; // California could be from (NSString *)placemark.administrativeArea;
NSString *abbr = [StateAbbreviation allStates:upperCase];
NSLog(@"%@", abbr); // Result should be CA

这只是您可以研究所有类似这样的状态的示例,状态及其缩写也喜欢这种状态及其缩写

于 2012-06-19T20:35:30.940 回答
3

我相信文档是不正确的。行政区域总是要返回美国地方的全州名称。要获得州缩写,您很可能必须创建一个字典查找表,以便搜索键“California”将返回值“CA”。

于 2012-06-19T20:30:38.547 回答
1

这是另一个使用 FormattedAddressLines 的类别,它返回类似加利福尼亚州的结果

-(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;
}

不完美,但只要 Apple 改变我认为的地址行格式,它就可以工作。

于 2013-02-06T04:00:08.267 回答
1

对于需要交换对象和键的状态列表的人(例如,在 iOS 7 上,我从 得到“CA” placemark.administrativeArea):

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];
于 2014-03-17T23:50:18.950 回答
0

至少从 iOS 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
于 2015-09-21T17:20:40.400 回答
0

字典的 SWIFT 变体

let states = [
    "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"
]
于 2020-08-07T14:42:35.553 回答