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];
Shmidt
  • 16,436
  • 18
  • 88
  • 136
  • 2
    The Country key is in quotes but shouldn't be. See [this question](http://stackoverflow.com/q/1923525/467105). –  Oct 17 '11 at 02:58
  • I just show different variants, none of them work – Shmidt Oct 17 '11 at 07:28
  • 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? –  Oct 17 '11 at 10:54
  • MKplacemark works, but doesn't show address when I tap on it. – Shmidt Oct 17 '11 at 14:16
  • I commented the address lines except one to test and now it works. Strange. Right syntax is: "location.street, kABPersonAddressStreetKey," – Shmidt Oct 18 '11 at 09:38

2 Answers2

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
                              };
Stephen
  • 741
  • 8
  • 18
Jay
  • 807
  • 10
  • 9
  • 2
    If you are using ARC you need to add `__bridge` to the cast, like: `(__bridge NSString *) kABPersonAddressStreetKey : location.street` – Diogo T May 29 '14 at 19:30
  • Don't forgot to #import (<=8.x) #import (>=9.x) – Mathi Arasan Nov 28 '16 at 06:37
  • 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. :( – Peter Brockmann Jun 11 '17 at 20:58
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
mavilein
  • 11,648
  • 4
  • 43
  • 48
David Douglas
  • 10,377
  • 2
  • 55
  • 53