2012-06-19 75 views
18

我想知道是否有可能從CLPlacemark得到狀態縮寫?CLPlacemark - 狀態縮寫?

在Apple的CLPlacemark參考它指出:

administrativeArea 與標的相關的州或省。 (只讀) @property(非原子,只讀)NSString * administrativeArea 討論 例如,如果地標位置是Apple的總部,則此屬性的值將是字符串「CA」或「California」。

但是每當我使用它時,我只能得到完整狀態(即California)而不是縮寫(即CA)。有人能幫我一下嗎?

+1

的[CLPlacemark-StateAbbreviation(https://github.com/jweyrich/CLPlacemark-StateAbbreviation)正是這麼做的。 – jweyrich

回答

32

對於需要此解決方案的其他人,我爲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

謝謝!我認爲這對未來的用戶/訪問者來說非常有用! – sridvijay

+1

感謝您在原始問題後很長一段時間內接受此問題......希望是的,它確實對某人有用。 – ChrisH

+2

嘿,我把它變成了一個還包括加拿大省份的plist。有點清潔+你可以添加更多(只需從文件加載字典,而不是寫入代碼)。這種方法雖然很棒。 http://cl.ly/3c0W1s3E1J1Y – shim

3

我相信該文檔是不正確的。 AdministrativeArea總是要返回美國地區的完整州名。要獲得州名縮寫,您最有可能必須創建一個字典查找表,以便搜索關鍵字「California」將返回值「CA」。

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

+1

謝謝!但是,你能否更新代碼來展示如何通過administrativeArea實現這一點?我只是把它放在像objectForKey之類的東西嗎?我對如何做到這一點感到困惑。感謝你的回答! – sridvijay

+0

檢查上面的代碼 – dianz

+0

感謝您的更新,但我得到2個錯誤,而這樣做。 1.在類方法中訪問的實例變量'strAbbreviation'。 2. ARC中不允許將NSUInteger隱式轉換爲NSString *。我拿出發佈聲明使其在ARC中工作,但我不知道如何解決這些問題。2.謝謝你。爲了幫助,我欣賞它。 – sridvijay

1

下面是使用FormattedAddressLines另外一類,它返回一個結果如加利福尼亞, 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 Cha更改我認爲的地址線的格式。

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]; 
1

由於至少的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 
+1

至少根據Apple提供的最新文檔,這不是真實的陳述: 此屬性中的字符串可以是行政區域的拼寫名稱或其指定的縮寫(如果存在)。例如,如果地標位置是Apple的總部,則此屬性的值將是字符串「CA」或「California」。 –