Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]; 
like image 598
Shmidt Avatar asked Oct 16 '11 18:10

Shmidt


1 Answers

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                               }; 
like image 67
Jay Avatar answered Sep 28 '22 04:09

Jay