objective-c - 如何为 MKPlacemark 创建 addressDictionary?

标签 objective-c ios5 mkannotation addressbook

    placemark = [[MKPlacemark alloc]initWithCoordinate:storedCoordinate addressDictionary:addressDict];

我尝试创建字典以用于上面的代码,但没有任何效果:(

    NSDictionary *addressDict = [[NSDictionary alloc] initWithObjectsAndKeys:
    location.countryCode, @"CountryCode",
    location.country,@"kABPersonAddressCountryKey", 
    location.state, kABPersonAddressStateKey, 
    location.city, @"City",
    location.street, kABPersonAddressStreetKey,
    location.zip, kABPersonAddressZIPKey,
    nil];

最佳答案

在为 MKPlacemark 创建 addressDictionary 时,建议您使用 ABPerson 中定义的“地址属性”常量。请注意,由于这些常量是 CFStringRef 类型,因此您需要将它们转换为 (NSString *) 以便将它们用作 NSDictionary 中的键。

NSDictionary *addressDict = @{
                              (NSString *) kABPersonAddressStreetKey : location.street,
                              (NSString *) kABPersonAddressCityKey : location.city,
                              (NSString *) kABPersonAddressStateKey : location.state,
                              (NSString *) kABPersonAddressZIPKey : location.zip,
                              (NSString *) kABPersonAddressCountryKey : location.country,
                              (NSString *) kABPersonAddressCountryCodeKey : location.countryCode
                              };

iOS 9+ 更新:使用新的联系人框架

NSDictionary *addressDict = @{
                              CNPostalAddressStreetKey : location.street,
                              CNPostalAddressCityKey : location.city,
                              CNPostalAddressStateKey : location.state,
                              CNPostalAddressPostalCodeKey : location.zip,
                              CNPostalAddressCountryKey : location.country,
                              CNPostalAddressISOCountryCodeKey : location.countryCode
                              };

关于objective-c - 如何为 MKPlacemark 创建 addressDictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7786276/

相关文章:

ios - 无法进行正则表达式匹配

ios - 在 UIWebView/WKWebView 中将 CSS 插入加载的 HTML

iOS : App crashes when zooming out a map

ios - 多行文本的选定 UITextRange 调整的 CGRect?

ios - 解雇ViewControllerAnimated :completion:后如何访问firstViewController的webView

wifi - iOS5中需要ARC的可达性版本

ios5 - 如何在iTunes Connect中更改应用程序名称

Swift 注释位置更新

ios - Swift - 从坐标获取地址

objective-c - 拖动 View