Reverse decoder "timing" issue in iOS 5


#1

I am trying to complete the Reverse Geocoding challenge. I’m using Xcode 4.2.1 and deployment target 5.0.

Since MKReverseGeocoder is deprecated for iOS5 I am using CLGeocoder and have researched how to use it using the docs and GeocoderDemo sample code.

The problem I am having is that the first time I add an annotation to the running app the annotation shows up with the title text but the placemark subtitle does not show up with the results from invoking the geocoder. The second time I create an annotation the subtitle displays but I think that the subtitle text is actually from the initial reverse geocoding. It appears that the geocoder block (evidenced by log statements I have within the geocoded block0 don’t get executed the first time through until after I tap the place mark added to the map view .

If I comment out the geocoder block and just assign a test string as in mp.subtitle = @“Test”;, the subtitle appears in the annotation correctly the first time through. So, it appears that there must be some kind of timing issue in executing the geocoder block that I don’t understand.

What’s going wrong, i.e., what am I missing in the docs or miscoding?

Here is my foundLocation method:

[code]- (void)foundLocation:(CLLocation *)loc
{
CLLocationCoordinate2D coord = [loc coordinate];

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

// Create an instance of MapPoint with the current data
MapPoint *mp = [[MapPoint alloc] initWithCoordinate:coord title:[locationTitleField text]];

[geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) {
    NSLog(@"reverseGeocodeLocation:completionHandler: Completion Handler called!");
    if (error){
        NSLog(@"Geocode failed with error: %@", error);
        [self displayError:error];
        return;
    }
    NSLog(@"Received placemarks: %@", placemarks);
    
    CLPlacemark *pm = [placemarks objectAtIndex:0];
    
    NSLog(@"subthoroughfare: %@", pm.subThoroughfare);
    NSLog(@"thoroughfare: %@", pm.thoroughfare);
    
    NSString *addressString = [NSString stringWithFormat:@"%@ %@", pm.subThoroughfare, pm.thoroughfare];
    
    as = addressString;
    
    NSLog(@"addressString: %@", addressString);
    
}];

// assign address string to subtitle ivar declared in .h as "addressString" is now out of scope of geocoder block
mp.subtitle = as;
// Add annotation to the map view
[worldView addAnnotation:mp];

[locationTitleField setText:@""];
[activityIndicator stopAnimating];
[locationTitleField setHidden:NO];
[locationManager stopUpdatingLocation];

}[/code]


#2

Move your mp.subtitle = as; inside the completion handler block.


#3

That works. I had tried that previously but for some reason it wasn’t working for me in previous testing.

Thanks for the reply.