使用CLGeocoder反向地理编码

时间:2012-02-23 07:53:37

标签: objective-c location exc-bad-access mkreversegeocoder

我想在获取用户当前位置的反向地理编码解决方案中寻求第二意见:

 - (void)reverseGeocodeLocation:(CLLocation *)location
{ 
    CLGeocoder* reverseGeocoder = [[CLGeocoder alloc] init];
    if (reverseGeocoder) {
        [reverseGeocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
            CLPlacemark* placemark = [placemarks firstObject];
            if (placemark && [placemark count] > 0) {
                 //Using blocks, get zip code
                 NSString *zipCode = [placemark.addressDictionary objectForKey:(NSString*)kABPersonAddressZIPKey];
            }
        }];
    }
    else{
        MKReverseGeocoder* revGeo = [[MKReverseGeocoder alloc] initWithCoordinate:location.coordinate];
        revGeo.delegate = self;//using delegate
        [revGeo start];
        [revGeo release]; 
    }
    [reverseGeocoder release];
}

但是,似乎有点问题......我遇到了EXC_BAD_ACCESS错误指向:

[reverseGeocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
            CLPlacemark* placemark = ...
        }];
你可以告诉我出了什么问题吗?我收到了EXC_BAD_ACCESS错误。

1 个答案:

答案 0 :(得分:1)

您没有检查发送到块的错误。您假设您至少返回了一个地标,但由于某种原因,该数组可能是空的。这当然可能是您的EXC_BAD_ACCESS错误的来源。

相关问题