Question

I use CLGeocoder to get the GPS coordinates for locations downloaded from a web-based API. Right now I count the number of records downloaded and update the MapView after I have iterated through my loop that number of times. This works, but it seems like there should be a more elegant / efficient method for doing this.

Any thoughts on the best way to accomplish this?

- (void) getCoordinatesForTheseLocations:(NSArray*) meetRecords
{
    NSInteger countOfAnnotations = [meetRecords count];

    if (debug==1) NSLog(@"countOfAnnotations equals %ld", (long)countOfAnnotations);
    NSInteger __block iCount = 0;
    for (NSDictionary* meet in meetRecords) {

    {
        NSString *location = [NSString stringWithFormat:@"%@, %@, %@",
                              [[meet objectForKey:MEET_VENUE] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]],
                              [[meet objectForKey:MEET_CITY] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]],
                              [[meet objectForKey:MEET_STATE_ABBREV] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];

        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:location completionHandler:^(NSArray *placemarks, NSError *error) {
            CLLocationCoordinate2D coordinates;
            CLPlacemark* bestGuess;

            if (error) {  
            ….} else if(placemarks && placemarks.count > 0) {

               NSMutableDictionary* meetInfoForAnnotation = [[NSMutableDictionary alloc] init];

                if (debug==2) NSLog(@"meet = %@", meet);

                meetInfoForAnnotation = [meet mutableCopy];

                if (debug==2) NSLog(@"Received placemarks: %@", placemarks);


                bestGuess = [placemarks objectAtIndex:0];
                coordinates.latitude = bestGuess.location.coordinate.latitude;
                coordinates.longitude = bestGuess.location.coordinate.longitude;

                [meetInfoForAnnotation setObject:[NSNumber numberWithDouble:coordinates.longitude] forKey:MEET_LONGITUDE];
                [meetInfoForAnnotation setObject:[NSNumber numberWithDouble:coordinates.latitude] forKey:MEET_LATITUDE];

                [self mapAnnotations:meetInfoForAnnotation];

            }

            if (iCount == countOfAnnotations - 1) {  //Update mapView if this is true
                dispatch_async(dispatch_get_main_queue(), ^{

                    myPinColor = MKPinAnnotationColorPurple;

                    [self setAnnotations:self.meetAnnotations]; 
                });
            }
            if (debug==1) NSLog(@"iCount = %d and countOfAnnotations = %d",iCount, countOfAnnotations);
            iCount ++;  //Increment counter whether are successful or have an error.
        }];
    }
}
Was it helpful?

Solution

The answer turns out to be simple. Never try to reinvent the wheel! I ended up using an api similar to that at Zip Code Distance API. I can then filter addresses based upon the zip code and stay within download limits.

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