First, in the addPinToMap:
method, addressLocation
is called with currentLocation
but currentLocation
is never set. It's declared a few lines up but not set to any value.
So change:
CLLocation *currentLocation;
to:
CLLocation *currentLocation = [[CLLocation alloc]
initWithLatitude:touchMapCoordinate.latitude
longitude:touchMapCoordinate.longitude];
Second, even with this fix, it still won't work. The annotation's title
will not get set because the reverseGeocodeLocation
method's completion handler block will finish after the annotation has already been added (the block is asynchronous -- the code in addPinToMap:
will not wait for it to finish).
You'll need to change the code around a bit and add the annotation inside the completion block when you actually have the geocoder result (whether success or failure).
Move the reverseGeocodeLocation
call to the addPinToMap:
method:
CLLocation *currentLocation = [[CLLocation alloc]
initWithLatitude:touchMapCoordinate.latitude
longitude:touchMapCoordinate.longitude];
[self.geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemark, NSError *error) {
//initialize the title to "unknown" in case geocode has failed...
NSString *annTitle = @"Address unknown";
//set the title if we got any placemarks...
if (placemark.count > 0)
{
CLPlacemark *topResult = [placemark objectAtIndex:0];
annTitle = [NSString stringWithFormat:@"%@ %@ %@ %@", topResult.country, topResult.locality, topResult.subLocality, topResult.thoroughfare];
}
//now create the annotation...
MapAnnotation *toAdd = [[MapAnnotation alloc]init];
toAdd.coordinate = touchMapCoordinate;
toAdd.title = annTitle;
toAdd.subtitle = @"Subtitle";
[self.map addAnnotation:toAdd];
}];