CLGeocoder仅返回第一个查找结果

时间:2014-04-30 21:19:01

标签: ios mkmapview clgeocoder

- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;我遇到了一个奇怪的问题。

我有一个需要查找的位置列表。所以我做了类似

的事情
for (Trip *trip in takenTrips) {
    [geoCoder geocodeAddressString:trip.location completionHandler:^(NSArray *placemarks, NSError *error) {
        //handling result
    }];
}

但似乎完成处理程序只被调用一次。我尝试按顺序进行这些查找。一切正常。

我搜索了一会儿,但找不到类似的东西。我在这里完全不知所措......

欢迎任何建议!

2 个答案:

答案 0 :(得分:0)

应用程序应该意识到它们如何使用地理编码。有效使用此类的经验法则:对任何一个用户操作最多发送一个地理编码请求。 class link

要解决您的问题,您可以选择以下任一项:

  1. 检查地理编码器是否正在处理地理编码的执行循环
  2. NSOperationQueue如果完成则会逐一取消
  3. 在completeHandle中执行运行下一个地理编码的回调

答案 1 :(得分:0)

为什么不使用更多地理编码器

dispatch_queue_t geocoderQueue = dispatch_queue_create("geocoder.queue", DISPATCH_QUEUE_CONCURRENT); 
dispatch_apply([takenTrips count], dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ull), ^(size_t index) {

    NSString *loc = [(Trip *)takenTrips[index] location];

    CLGeocoder *geoCoder = [CLGeocoder new];
    [geoCoder geocodeAddressString:loc completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark = [placemarks firstObject];
        NSLog(@"%@",placemark.postalCode);
    }];
});

以及私有并发队列,这样您就可以获得出色的效果。

相关问题