Forward Geocoding with CLGeocoder Issues

1.1k views Asked by At

I'm having an issue using forward geocoding on iOS5 with the geocodeAddressString function.

I have a MKMapView open up as a modal view to display different locations with data grabbed from the internet. My issue is when I dismiss the modal view, and then open it up again. On second try, only about half of my annotations are actually placed on the map. On a third try, none show up.

I have a feeling my issue here has to do with memory management and the scope of the CLGeocoder, but I just can't figure it out.

I create all of my annotations in the ViewDidLoad function of the view controller containing the MapView. Here is the code I use to get the coordinates of the addresses:

int locationCount = 0;
for(NSDictionary *date in locations)
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:[NSString stringWithFormat:@"%@, %@", [date objectForKey:@"venue"], [date objectForKey:@"location"]] completionHandler:^(NSArray *placemarks, NSError *error)
    {
        // if we find the exact location (including the venue string), create an annotation for the map
        if(placemarks && placemarks.count > 0)
        {
            CLPlacemark *topResult = [placemarks objectAtIndex:0];
            TourAnnotation *placemarkAnnotation = [[TourAnnotation alloc] initWithLocation:topResult.location andDetails:date];
            placemarkAnnotation.tag = locationCount;
            [tourMap addAnnotation:placemarkAnnotation];
        }
        // if not place an annotation at the center of the city
        else
        {
            [geocoder geocodeAddressString:[date objectForKey:@"location"] completionHandler:^(NSArray *placemarks, NSError *error)
             {
                 if(placemarks && placemarks.count > 0)
                 {
                     CLPlacemark *topResult = [placemarks objectAtIndex:0];
                     TourAnnotation *placemarkAnnotation = [[TourAnnotation alloc] initWithLocation:topResult.location andDetails:date];
                     placemarkAnnotation.tag = locationCount;
                     [tourMap addAnnotation:placemarkAnnotation];
                 }
             }];
        }
    }];
    ++locationCount;
}

Any suggestions would be appreciated.

1

There are 1 answers

0
SmokersCough On

If it's memory issues have you considered how the annotation are dequeued and reused, the map view has a delegate method to do such a thing.

This tutorial may be of some assistance.

Ray Wenderlich map tutorial