我使用CLGeocoder
获取从基于网络的API下载的位置的GPS坐标。现在,我计算了下载的记录数,并在迭代了我的循环次数后更新了MapView。这可行,但似乎应该有一个更优雅/有效的方法来做到这一点。
有关实现这一目标的最佳方法的任何想法吗?
- (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.
}];
}
}
答案 0 :(得分:1)
答案结果很简单。永远不要试图重新发明轮子!我最终使用了与Zip Code Distance API类似的api。然后,我可以根据邮政编码过滤地址,并保持在下载限制范围内。