对许多地址进行地理编码 - CLGeocoder

eul*_*ulr 2 objective-c core-location mapkit cllocationmanager

我需要使用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...
Run Code Online (Sandbox Code Playgroud)

以下是我的日志显示方式:

here
here
here
here
.
..
...
....
<MKPointAnnotation: 0x16fc12e0>
Run Code Online (Sandbox Code Playgroud)

因此,尽管使用循环调用该函数,但该块不会针对除第一个之外的任何结果运行.

我知道我的地址很好.这对于函数的任何数量(> 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);
}
Run Code Online (Sandbox Code Playgroud)

...仍然会拉出一张没有注释的空白地图 - 甚至不是第一个.每次调用函数时,地理编码器阵列都有不同的内存地址,因此该部分似乎正在工作.

wat*_*n12 5

你连续几次调用geocodeAddressString,但苹果的文档说

在发起前向地理编码请求后,请勿尝试启动另一个前向或反向地理编码请求.

所以我认为正在发生的事情是当你创建一个新请求时取消当前请求,或者如果一个新请求已在进行中,则忽略所有新请求.

作为一种解决方法,您可以对一个点进行地理编码,然后对完成块中的下一个点进行地理编码或创建多个地理编码器实例