CLPlacemark - 状态缩写?

时间:2012-06-19 19:57:18

标签: iphone ios clgeocoder

我想知道是否有可能从CLPlacemark获得州名缩写?

在Apple的CLPlacemark参考中,它声明:

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

但每当我使用它时,我只获得完整状态(即加利福尼亚州)而不是缩写(即CA)。谁能在这帮助我?

7 个答案:

答案 0 :(得分:33)

对于其他需要解决方案的人来说,我已经为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

答案 1 :(得分:6)

我认为你不能得到各州的缩写,但你可以为此制作自己的班级。

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

<强>代码..

类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

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

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

这只是您可以研究所有状态的示例,states and their abbreviations也是这样states and their abbreviations

答案 2 :(得分:3)

我认为文档不正确。 administrativeArea总是返回美国地方的完整州名。要获得州名缩写,您很可能必须创建一个字典查找表,以便搜索“加利福尼亚”键将返回值“CA”。

答案 3 :(得分:1)

这是使用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;
}

不完美但只要Apple改变我认为的地址行格式就行。

答案 4 :(得分:1)

对于需要交换对象和密钥的状态列表的人(例如在iOS 7上我得到&#34; CA&#34;来自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];

答案 5 :(得分:1)

至少从iOS 8开始,CLPlacemark的{​​{1}}会返回美国国家/地区的两个字母缩写。

只要你的目标是iOS 8及更新版本(你现在应该是这样),你就不需要像接受答案中那样扩展CLPlacemark类别。

administrativeArea

运行这个,你会得到:

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);
}];

答案 6 :(得分: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"
]