0
我使用CLGeocoder
來獲取從基於Web的API下載的位置的GPS座標。現在我計算下載的記錄數並在遍歷循環次數後更新MapView。這有效,但似乎應該有一個更優雅/有效的方法來做到這一點。更新MKMapView的適當方法
有關完成此操作的最佳方法的任何想法?
- (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.
}];
}
}