Question

Here is my code and the full context in which I am trying to call reverseGeocodeLocation:

CLLocationCoordinate2D touchMapCoordinate = 
[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];

CLLocation *destination = [[CLLocation alloc] initWithLatitude:touchMapCoordinate.latitude longitude:touchMapCoordinate.longitude];

__block CLGeocoder *destinationPin = [[CLGeocoder alloc] init];
__block NSString *destinationPinName;

[destinationPin reverseGeocodeLocation:destination completionHandler:^(NSArray *placemarks, NSError *error) {
    if (error){
        NSLog(@"Geocode failed with error: %@", error);
        return;
    }
    if(placemarks && placemarks.count > 0)
    {
        CLPlacemark *topResult = [placemarks objectAtIndex:0];

        NSString *addressTxt = [NSString stringWithFormat:@"%@ %@,%@ %@",
                                [topResult subThoroughfare],[topResult thoroughfare],
                                [topResult locality], [topResult administrativeArea]];

        destinationPinName = [[NSString alloc] initWithString:addressTxt];
    }
}];

annotation = [[MQAnnotation alloc] initWithTitle:destinationPinName andCoordinate:touchMapCoordinate];

I have breakpoints set up at the function entry points, within both if-blocks in the function, and after the function where I assign annotation. When I get to the reverseGeocodeLocation block destinationPin has a valid value, but my program keeps jumping straight to the break point set after the function call. Any ideas what might be happening??

Was it helpful?

Solution

The execution of reverseGeocodeLocation is done asynchronously. When that background task is complete, your completion block will be called. So what you are seeing is correct behavior. The call to reverseGeocodeLocation returns immediately, long before it is complete and long before your completion handler is called. You need to update your code so your annotation is created in the completion handler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top