iOS5 CLGeocoder

inf*_*ies 3 iphone ios objective-c-blocks ios5 clgeocoder

我正在为IOS5开发一款iPhone应用程序.我目前正在使用位于CoreLocation框架内的CLGeocoder类.在地理编码发生或同时发生后,我无法弄清楚最后是否调用了完成处理程序块.

我只知道完成处理程序块是在主线程上运行的.有没有人知道完成处理程序块是在地理编码完成时运行还是在地理编码器在另一个线程上执行时完成手头任务的代码?

nev*_*ing 6

在地理编码器完成地理编码后运行完成处理程序.换句话说,它在完成地理编码任务时运行.当地理编码器运行时,它不是用于完成其他任务.

完成处理程序包含地标和错误.如果地理编码成功,您将获得一个地标数组.如果没有,则会出错.

来自文档的说明:

此方法异步将指定的位置数据提交给地理编码服务器并返回.您的完成处理程序块将在主线程上执行.在发起前向地理编码请求后,请勿尝试启动另一个前向或反向地理编码请求.

地理编码请求对每个应用程序都是速率限制的,因此在短时间内发出太多请求可能会导致某些请求失败.超过最大速率时,地理编码器会将值为kCLErrorNetwork的错误对象传递给完成处理程序.

@interface MyGeocoderViewController ()

@property (nonatomic, strong) CLGeocoder *geocoder;

@end

@implementation MyGeocoderViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Create a geocoder and save it for later.
    self.geocoder = [[CLGeocoder alloc] init];
}
- (void)geocodeAddress:(NSString *)addressString
{
    // perform geocode
    [geocoder geocodeAddressString:addressString
        completionHandler:^(NSArray *placemarks, NSError *error) {

        if ((placemarks != nil) && (placemarks.count > 0)) {
            NSLog(@"Placemark: %@", [placemarks objectAtIndex:0]);
        }
        // Should also check for an error and display it
        else {
            UIAlertView *alert = [[UIAlertView alloc] init];
            alert.title = @"No places were found.";
            [alert addButtonWithTitle:@"OK"];
            [alert show];
        }
    }];
}

@end
Run Code Online (Sandbox Code Playgroud)