How to create addressDictionary for MKPlacemark?
Asked Answered
G

2

30
    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];
Gt answered 16/10, 2011 at 18:14 Comment(5)
The Country key is in quotes but shouldn't be. See this question.Talaria
I just show different variants, none of them workGt
Can you describe more how it doesn't work? For example, if it crashes what's the error message? If not crash then what exactly? What do you do with the placemark variable after the init?Talaria
MKplacemark works, but doesn't show address when I tap on it.Gt
I commented the address lines except one to test and now it works. Strange. Right syntax is: "location.street, kABPersonAddressStreetKey,"Gt
N
48

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
                              };
Naivete answered 26/11, 2012 at 21:3 Comment(3)
If you are using ARC you need to add __bridge to the cast, like: (__bridge NSString *) kABPersonAddressStreetKey : location.streetCerulean
Don't forgot to #import <AddressBook/AddressBook.h> (<=8.x) #import <Contacts/Contacts.h> (>=9.x)Dort
Although the new Contacts framework works in iOS 9, some APIs that consume them are available only in iOS 10. (MKPlacement initWithCoordinate:postalAddress, for example, works only in iOS 10), so you'll have to use the ABAdress approach. :(Basque
S
14

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
Scoundrelly answered 9/10, 2013 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.