CLPlacemark - 州缩写?

Posted

技术标签:

【中文标题】CLPlacemark - 州缩写?【英文标题】:CLPlacemark - State Abbreviations? 【发布时间】:2012-06-19 19:57:18 【问题描述】:

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

在 Apple 的 CLPlacemark Reference 中指出:

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

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

【问题讨论】:

CLPlacemark-StateAbbreviation 正是这样做的。 【参考方案1】:

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

【讨论】:

谢谢!我认为这对于看到此内容的未来用途/访问者可能非常有用! 感谢您在最初的问题之后这么长时间接受这个......希望是的,它确实对某人有用。 嘿,我把它变成了一个plist,其中还包括加拿大各省。更简洁+您可以添加更多内容(只需从文件中加载字典,而不是用代码编写)。这个方法虽然很棒。 cl.ly/3c0W1s3E1J1Y @shim - 很好。谢谢!【参考方案2】:

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

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

代码..

类状态缩写

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

【讨论】:

谢谢!但是您能否更新代码以显示如何使用管理区域来实现这一点?我只是把它放在 objectForKey 之类的东西中吗?我对如何做到这一点有点困惑。感谢您的回答! 感谢更新,但执行此操作时出现 2 个错误。 1. 在类方法中访问的实例变量“strAbbreviation”。 2. NSUInteger 到 NSString * 的隐式转换在 ARC 中是不允许的。我拿出了发布声明以使其在 ARC 中工作,但我不知道如何解决这两个问题。再次感谢。对于帮助,我很感激。 没问题,你能帮我解决这个问题吗:p?【参考方案3】:

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

【讨论】:

【参考方案4】:

这是另一个使用 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 改变我认为的地址行格式,它就可以工作。

【讨论】:

【参考方案5】:

对于需要交换对象和键的状态列表的人(例如,在 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];

【讨论】:

【参考方案6】:

至少从 iOS 8 开始,CLPlacemarkadministrativeArea 返回美国各州的两个字母缩写。

只要您的目标是 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

【讨论】:

至少根据 Apple 提供的最新文档,这不是一个真实的陈述:此属性中的字符串可以是行政区域的拼写名称或其指定的缩写,如果有的话存在。例如,如果地标位置是 Apple 的总部,则此属性的值将是字符串“CA”或“California”。【参考方案7】:

字典的 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"
]

【讨论】:

以上是关于CLPlacemark - 州缩写?的主要内容,如果未能解决你的问题,请参考以下文章

php 缩写州帐单/送货字段

美国州缩写词典及其转换

javascript 在JavaScript中将州名转换为缩写

地名自动完成城市/州,返回完整的州名而不是缩写

美国50个州下拉列表(完整和缩写)

所有美国州的名称缩写和县都是XML格式