2

我的核心数据中有一些地址,我需要获取它们的纬度和经度。地址的数量大约在 150 到 200 之间。我在iPhone iOS5 CLGeocoder 如何对一大组(200)地址集进行地理编码?. 我需要知道如何在那一分钟内触发 50 个地理编码,然后再触发 50 个,依此类推。执行代码我得到以下错误:

地理编码失败并出现错误:错误域 = kCLErrorDomain 代码 = 2“操作无法完成。(kCLErrorDomain 错误 2。)”

用户 nob1984 在我提到的线程中提出了一个可能的解决方案:

如果您需要一次对所有内容进行地理编码,则需要将地理编码与每个块之间的延迟链接在一起,并在完成之前显示某种忙碌指示符。使用大量地理编码可能需要一段时间。

但我不知道如何使这些延迟正常工作。欢迎任何帮助和建议!谢谢

这是代码:

...
NSArray *arrayOfRoutePoints = fetchedObjects;
[self geolocateGroup:arrayOfRoutePoints];
...

- (void)geolocateGroup:(NSArray *)array {
for (RoutePoint *rp in array) {
    if ([rp.addressLatitude intValue] == 0) {
        dispatch_group_t group = dispatch_group_create();
        dispatch_group_enter(group);

        [self geolocateRoutePoint:rp];

        dispatch_group_leave(group);

        while(dispatch_group_wait(group,DISPATCH_TIME_NOW)){
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                                     beforeDate:[NSDate dateWithTimeIntervalSinceNow:3.0f]];
        }
    }
}    
}     

- (void)geolocateRoutePoint:(RoutePoint *)c {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
NSString *addressString = [NSString stringWithFormat:@"%@,%@ %@ %@ %@",
                           c.addressName,
                           c.addressNumber,
                           @"SOME CITY",
                           @"SOME STATE",
                           @"SOME COUNTRY"];

[geocoder geocodeAddressString:addressString
             completionHandler:^(NSArray *placemarks, NSError *error) {

                 if (error) {
                     NSLog(@"Geocode failed with error: %@", error);
                     return;
                 }

                 if (placemarks && placemarks.count > 0)
                 {
                     CLPlacemark *placemark = placemarks[0];

                     CLLocation *location = placemark.location;
                     c.addressLatitude =  [NSNumber numberWithDouble:location.coordinate.latitude];
                     c.addressLongitude = [NSNumber numberWithDouble:location.coordinate.longitude];
                 }
             }];
}
4

2 回答 2

1

嗯,也许只是在你的 GCD 中使用“afterDelay”,而不是 NSDate。

[self performSelector:@selector(geolocateGroup) 
withObject:@"Grand Central Dispatch" afterDelay:3.0];
于 2013-01-28T19:57:29.307 回答
1

我在回复 Droidnoid 时想到了一个主意,它可以工作。

这是交易:递归。地理编码完成后,它会以 3 秒的时间间隔开始一个新的地理编码请求。我现在变成了只有一种方法。

...
NSMutableArray *arrayOfRoutePoints = [[NSMutableArray alloc] initWithArray:fetchedObjects];
[self geolocateRoutePoint:arrayOfRoutePoints];
...

- (void)geolocateRoutePoint:(NSMutableArray *)mutableArrayWithRoutePoints {

if ([mutableArrayWithRoutePoints count] > 0) {
    RoutePoint *c = [mutableArrayWithRoutePoints objectAtIndex:0];

    if ([c.addressLatitude intValue] == 0) {
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        NSString *addressString = [NSString stringWithFormat:@"%@,%@ %@ %@ %@",
                                   c.addressName,
                                   c.addressNumber,
                                   @"SOME CITY",
                                   @"SOME STATE",
                                   @"SOME COUNTRY"];

        [geocoder geocodeAddressString:addressString
                     completionHandler:^(NSArray *placemarks, NSError *error) {

                         [mutableArrayWithRoutePoints removeObjectAtIndex:0];
                         [self performSelector:@selector(geolocateRoutePoint:)
                                    withObject:mutableArrayWithRoutePoints afterDelay:3.0];

                         if (error) {
                             NSLog(@"Geocode failed with error: %@", error);
                             return;
                         }

                         if (placemarks && placemarks.count > 0)
                         {
                             CLPlacemark *placemark = placemarks[0];

                             CLLocation *location = placemark.location;
                             c.addressLatitude =  [NSNumber numberWithDouble:location.coordinate.latitude];
                             c.addressLongitude = [NSNumber numberWithDouble:location.coordinate.longitude];

                             // Saving the context here after getting geocode.
                             // Definitely not the best place to save the context. 
                             // I will move this later to another place
                             NSError *error = nil;
                             if (![self.managedObjectContext save:&error]) {
                                 NSLog(@"Error! %@", error);
                             }
                         }
                     }];
    }
    else
    {
        [mutableArrayWithRoutePoints removeObjectAtIndex:0];
        [self performSelector:@selector(geolocateRoutePoint:)
                   withObject:mutableArrayWithRoutePoints];
    }
}
}
于 2013-01-28T23:40:51.470 回答