【发布时间】:2013-11-07 14:23:49
【问题描述】:
我需要使用 CLGeocoder 对大量地址进行地理编码,不是 Google API。
我的问题是我的代码只是为下面数组中的第一个地址添加注释。但是,该函数被调用了适当的次数 - 在为所有地址调用该函数之后,该块仅针对第一个地址运行:
- (void)mapPoints {
NSString *foo = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"names%d", self.indexOfSchool] ofType:@"plist"];
NSArray *description = [NSArray arrayWithContentsOfFile:foo];
NSString *bar = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d1", self.indexOfSchool] ofType:@"plist"];
NSArray *details = [NSArray arrayWithContentsOfFile:bar];
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d0", self.indexOfSchool] ofType:@"plist"];
NSArray *addresses = [NSArray arrayWithContentsOfFile:path];
self.saved = [NSMutableArray alloc] init];
for (int q = 0; q < addresses.count; q+= 20) {
[self annotate:[addresses objectAtIndex:q] detail:[details objectAtIndex:q] andDesc:[description objectAtIndex:q]];
}
}
see updated annotate: method below...
以下是我的日志的显示方式:
here
here
here
here
.
..
...
....
<MKPointAnnotation: 0x16fc12e0>
因此,虽然该函数是通过循环调用的,但该块并未针对除第一个结果之外的任何结果运行。
我知道我的地址很好。这不适用于任何数量 (>1) 的函数调用。
循环中的 += 20 与仅绘制一个点无关:这对 ++ 也不起作用(也有大约 200 个结果)。
我对块相当陌生,所以希望这是一个简单的解决方法。非常感谢任何帮助!
编辑 改为这样做:
- (void)annotate:(NSString *)address detail:(NSString *)detail andDesc:(NSString *)description {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:address
completionHandler:^(NSArray* placemarks, NSError* error) {
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = placemark.coordinate;
point.title = description;
point.subtitle = [NSString stringWithFormat:@"%@ miles from origin", detail];
MKCoordinateRegion region = self.directionsView.region;
region.center = [(CLCircularRegion *)placemark.region center];
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;
[self.directionsView addAnnotation:point];
NSLog(@"%@", point);
[self.directionsView setRegion:region animated:YES];
}
}
];
[self.saved addObject:geocoder];
NSLog(@"%@", geocoder);
}
... 仍然拉出一张没有注释的空白地图——甚至不是第一个。每次调用该函数时,地理编码器数组都有不同的内存地址,因此该部分似乎正在工作。
【问题讨论】:
-
我认为你应该在 GCD 中使用信号量。
标签: objective-c mapkit core-location cllocationmanager