ios - 在 iOS 中使用正向地理编码获取多个地址的纬度和经度时如何添加延迟?

标签 ios objective-c geocoding delay

我的核心数据中有一些地址,我需要获取它们的纬度和经度。地址数量约为 150 到 200 个。我了解到 iOS 在线程 iPhone iOS5 CLGeocoder how to geocode a large (200) set of addresses? 中限制每分钟 50 个地理编码。 。我需要知道如何在那一分钟内触发 50 个地理编码,然后再触发另外 50 个,依此类推。执行代码时出现以下错误:

Geocode failed with error: Error Domain=kCLErrorDomain Code=2 "The operation couldn’t be completed. (kCLErrorDomain error 2.)"

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

If you need to have everything geocoded at once, you'll need to chain your geocodings together with delays between each chunk and show some sort of busy indicator until you are done. Which could be some time with large sets of geocodings.

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

这是代码:

...
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];
                 }
             }];
}

最佳答案

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

[self performSelector:@selector(geolocateGroup) 
withObject:@"Grand Central Dispatch" afterDelay:3.0];

关于ios - 在 iOS 中使用正向地理编码获取多个地址的纬度和经度时如何添加延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14556840/

相关文章:

ios - 使用 xcodebuild 指定单独的目标build设置

geocoding - 如何使用反向地理编码获取 Mapbox 的城市/州/国家/地区?

ios - 如何在 Google MapView iOS Swift 4 上呈现来自 Web Api 的地址数组

Objective-C:在哪里删除 NSNotification 的观察者?

objective-c - 在非主线程中运行 Cocoa GUI

objective-c - 将 openGL ES 纹理读取到原始数组

iOS 如何在 2015 年将地址转换为 GPS 坐标(地理编码)?

ios - 将来自 API 的数组的数组信息显示为表格

ios - 来自苹果的 APNS 警告,即使我没有在我的应用程序中使用 APNS

ios - 类型 'Data' 的值没有成员 'writeToURL'