使用CLGeocoder解码地址时,遇到错误 Error Domain=kCLErrorDomain Code=2
代码:
#pragma mark 跟踪定位代理方法,每次位置发生变化即会执行(只要定位到相应位置)-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation *location=[locations firstObject];//取出第一个位置 CLLocationCoordinate2D coordinate=location.coordinate;//位置坐标 NSLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed); CLGeocoder *gcode = [[CLGeocoder alloc]init]; [gcode reverseGeocodeLocation:location completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) { if (error) { NSLog(@"%@",error); }else { CLPlacemark *mk = [placemarks objectAtIndex:0]; NSString *state = [mk.addressDictionary objectForKey:@"State"]; NSString *city = [mk.addressDictionary objectForKey:@"City"]; NSString *lity = [mk.addressDictionary objectForKey:@"SubLocality"]; NSString *stret = [mk.addressDictionary objectForKey:@"Street"]; NSString *name = [mk.addressDictionary objectForKey:@"Name"]; } isGettingLocation = NO; }]; //如果不需要实时定位,使用完即使关闭定位服务 [_locationManager stopUpdatingLocation];}
错误原因:
地址解析需要网络,检查网络连接!手机是否断网了
http://stackoverflow.com/questions/29087660/error-domain-kclerrordomain-code-2-the-operation-couldn-t-be-completed-kclerr