I'm trying to understand CLPlacemark and when/how to create information for a callout of a pin that is added to a map. Before what I read in More iOS 3 development a few years ago, they reverse geocoded an address and built the address (street, zip, state, etc). First, do I need to build this string myself? I was trying to find out how to get the name of a location for certain known things like searching for the apple store in the code below:
NSString *address = @"1 stockton, san francisco, ca";
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
[placemarks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"obj description: %@", [obj description]);
CLPlacemark *aPlacemark = (CLPlacemark *)obj;
NSLog(@"%@", [aPlacemark.addressDictionary description]);
NSLog(@"name: %@", ((CLPlacemark *)obj).name);
}];
];
When I print out the description, I see that the Console says:
Apple Store, San Francisco, 1 Stockton St, San Francisco, CA 94108-5805, United States @ <+37.78584545,-122.40651752> +/- 100.00m, region (identifier <+37.78584545,-122.40652161> radius 18.96) <+37.78584545,-122.40652161> radius 18.96m
Where does it get the Apple Store, San Francisco, name? I thought it would be the CLPlacemark.name property, but that is null. So in trying to figure out how the name property is created, I found:
NSLog(@"%@", [aPlacemark.addressDictionary description]);
I get the output:
City = "San Francisco";
Country = "United States";
CountryCode = US;
FormattedAddressLines = (
"Apple Store, San Francisco",
"1 Stockton St",
"San Francisco, CA 94108-5805",
"United States"
);
PostCodeExtension = 5805;
State = California;
Street = "1 Stockton St";
SubAdministrativeArea = "San Francisco";
SubLocality = "Union Square";
SubThoroughfare = 1;
Thoroughfare = "Stockton St";
ZIP = 94108;
From this, all I can see is that in the FormattedAddressLines key of the addressDictionary, the title is there as well.
So I guess my 2 questions are:
1) How do I get the name of a location if there is one (i.e. Apple Store)?
2) Do I need to build my string anymore since it seems like the address dictionary does that for me already?
Thanks!
You can use ABCreateStringWithAddressDictionary function from AddressBookUI framework to get the address string from CLPlacemark's "addressDictionary" property.