Search code examples
objective-cios5mkannotationaddressbook

How to create addressDictionary for MKPlacemark?


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

Solution

  • 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
                                  };