如何为 MKPlacemark 创建地址字典?

发布于 2024-12-10 05:16:44 字数 535 浏览 0 评论 0原文

    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];
    placemark = [[MKPlacemark alloc]initWithCoordinate:storedCoordinate addressDictionary:addressDict];

I tried to create dictionary to use for code above, but nothing works :(

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

徒留西风 2024-12-17 05:16:44

为 MKPlacemark 创建地址字典时,建议您使用 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
                              };

When creating the addressDictionary for the MKPlacemark, it's recommended that you use the "Address Property" constants as defined within ABPerson. Note, since these constants are of type CFStringRef, so you will need to cast them to an (NSString *) in order to use them as keys within the 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
                              };

Update for iOS 9+: Use new Contacts Framework

NSDictionary *addressDict = @{
                              CNPostalAddressStreetKey : location.street,
                              CNPostalAddressCityKey : location.city,
                              CNPostalAddressStateKey : location.state,
                              CNPostalAddressPostalCodeKey : location.zip,
                              CNPostalAddressCountryKey : location.country,
                              CNPostalAddressISOCountryCodeKey : location.countryCode
                              };
一抹微笑 2024-12-17 05:16:44

值得注意的是,您需要将“AddressBook.framework”添加到项目构建设置中。
还要导入您的标头(.h 文件):

#import <AddressBook/AddressBook.h>

然后在您的实现(.m 文件)中您可以使用:

(NSString*)kABPersonAddressStreetKey
(NSString*)kABPersonAddressCityKey
(NSString*)kABPersonAddressStateKey
(NSString*)kABPersonAddressCountryKey

Worth noting that you will need to add the 'AddressBook.framework' to your project build settings.
Also import in your header (.h file):

#import <AddressBook/AddressBook.h>

Then in your implementation (.m file) you can use:

(NSString*)kABPersonAddressStreetKey
(NSString*)kABPersonAddressCityKey
(NSString*)kABPersonAddressStateKey
(NSString*)kABPersonAddressCountryKey
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文