试图对地址进行地理编码并获取kCLErrorGeocodeFoundNoResult

时间:2014-08-25 02:46:30

标签: ios objective-c clgeocoder

所以我正在尝试GeoCode一个地址,当有人输入" asdfsfdsf"它会抛出错误

"kCLErrorGeocodeFoundNoResult"

如何捕获错误,以便它不会向用户显示丑陋的弹出窗口(即上面的错误)?

-(void)geocodePinAddress:(NSString *)address withBlock:(void (^)(CLLocationCoordinate2D coord))block {

CLGeocoder* gc = [[CLGeocoder alloc] init];
__block CLLocationCoordinate2D coord;

[gc geocodeAddressString:address completionHandler: ^(NSArray *placemarks, NSError *error) {

    // Check for returned placemarks
    if (placemarks && placemarks.count > 0) {
        CLPlacemark* mark = [placemarks objectAtIndex:0];

        coord = mark.location.coordinate;

        block(coord);

    }

}];
}

2 个答案:

答案 0 :(得分:3)

Here is how you can handle geocoder domain errors :


    if(placemarks.count > 0)
    {
       CLPlacemark *placemark = [placemarks objectAtIndex:0];
       self.outputLabel.text = placemark.location.description;
    }
    else if (error.domain == kCLErrorDomain)
    {
      switch (error.code)
      {
        case kCLErrorDenied:
           self.outputLabel.text = @"Location Services Denied by User";
           break;
        case kCLErrorNetwork:
           self.outputLabel.text = @"No Network";
           break;
        case kCLErrorGeocodeFoundNoResult:
           self.outputLabel.text = @"No Result Found";
           break;
        default:
           self.outputLabel.text = error.localizedDescription;
           break;
      }
    }

答案 1 :(得分:0)

为什么不在发生错误时显示消息?

- (void)geocodePinAddress:(NSString *)address withBlock:(void (^)(CLLocationCoordinate2D coord))block {
    CLGeocoder *gc = [[CLGeocoder alloc] init];
    __block CLLocationCoordinate2D coord;

    [gc geocodeAddressString:address completionHandler: ^(NSArray *placemarks, NSError *error) {
        // if there was some error geocoding
        if (error) {
            // display whatever message you want, however you want, here
            return;
        }

        // Check for returned placemarks
        if (placemarks && placemarks.count > 0) {
            CLPlacemark* mark = [placemarks objectAtIndex:0];

            coord = mark.location.coordinate;

            block(coord);
        }
    }];
}