简体   繁体   English

iOS5 CLGeocoder

[英]iOS5 CLGeocoder

I'm developing an iPhone app for IOS5. 我正在为IOS5开发一款iPhone应用程序。 I'm currently using the CLGeocoder class located within the CoreLocation framework. 我目前正在使用位于CoreLocation框架内的CLGeocoder类。 I cannot figure out if the completion handler block is called at the end, after the geocoding takes place or concurrently. 在地理编码发生或同时发生后,我无法弄清楚最后是否调用了完成处理程序块。

I only know that the completion handler block is ran on the main thread. 我只知道完成处理程序块是在主线程上运行的。 Does anyone know if the completion handler block is ran on completion of the geocoded or is the code for completing the task at hand while the geocoder executes on another thread? 有没有人知道完成处理程序块是在地理编码完成时运行还是在地理编码器在另一个线程上执行时完成手头任务的代码?

The completion handler is run after the geocoder has finished geocoding. 在地理编码器完成地理编码后运行完成处理程序。 In other words, it's run on completion of the geocoding task. 换句话说,它在完成地理编码任务时运行。 It's not for completing some other task while the geocoder runs. 当地理编码器运行时,它不是为了完成其他任务。

The completion handler contains the placemarks and an error. 完成处理程序包含地标和错误。 If geocoding was successful, you get a placemarks array. 如果地理编码成功,您将获得一个地标数组。 If not, you get an error. 如果没有,则会出错。

Notes from the docs: 来自文档的说明:

This method submits the specified location data to the geocoding server asynchronously and returns. 此方法异步将指定的位置数据提交给地理编码服务器并返回。 Your completion handler block will be executed on the main thread. 您的完成处理程序块将在主线程上执行。 After initiating a forward-geocoding request, do not attempt to initiate another forward- or reverse-geocoding request. 在发起前向地理编码请求后,请勿尝试启动另一个前向或反向地理编码请求。

Geocoding requests are rate-limited for each app, so making too many requests in a short period of time may cause some of the requests to fail. 地理编码请求对每个应用程序都是速率限制的,因此在短时间内发出过多请求可能会导致某些请求失败。 When the maximum rate is exceeded, the geocoder passes an error object with the value kCLErrorNetwork to your completion handler. 超过最大速率时,地理编码器会将值为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

The Apple documentation says: This method submits the specified location data to the geocoding server asynchronously and returns. Apple文档说:此方法异步将指定的位置数据提交给地理编码服务器并返回。 Your completion handler block will be executed on the main thread. 您的完成处理程序块将在主线程上执行。 After initiating a forward-geocoding request, do not attempt to initiate another forward- or reverse-geocoding request. 在发起前向地理编码请求后,请勿尝试启动另一个前向或反向地理编码请求。

No need to use GCD to dispatch the update on an IBOulet. 无需使用GCD在IBOulet上分发更新。

http://developer.apple.com/library/ios/#DOCUMENTATION/CoreLocation/Reference/CLGeocoder_class/Reference/Reference.html http://developer.apple.com/library/ios/#DOCUMENTATION/CoreLocation/Reference/CLGeocoder_class/Reference/Reference.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM