Question

I'm trying to iterate through my array which has a list of postcodes and then call the geocodeAddressString function to plot them on a MapView. Here's the code.

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


for (int i = 0; i < [[appDelegate offersFeeds] count]; i++)
{
    NSString *plotAddress = [[[appDelegate offersFeeds] objectAtIndex:i] valueForKey:@"addressline"];


    [geocoder geocodeAddressString:plotAddress completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"%@", plotAddress);
    }];        
}

For some reason only the first postcode in the array is been output. I would have expect them all to be as it loops through. Any ideas?

Was it helpful?

Solution

When you create a CLGeocoder object, you can only use it to geocode one address at a time. If you want multiple parallel requests, you need to create a CLGeocoder object for each request.

Here's one way to do it (assuming ARC is enabled):

for (int i = 0; i < [[appDelegate offersFeeds] count]; i++)
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    NSString *plotAddress = [[[appDelegate offersFeeds] objectAtIndex:i] valueForKey:@"addressline"];

    [geocoder geocodeAddressString:plotAddress completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"%@", plotAddress);
    }];        
}

Additional information...

Note that Apple may place restrictions on how many parallel requests you can have at once, so if you're doing a large number of requests, you'll need to do the geocoding in batches.

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