所以我正在尝试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);
}
}];
}
答案 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);
}
}];
}