Ios 刷新后的CALayer设置为“其他注释视图”

Ios 刷新后的CALayer设置为“其他注释视图”,ios,mapkit,calayer,mkannotationview,Ios,Mapkit,Calayer,Mkannotationview,对于一些注释视图,我设置了一些子层。首先加载子层匹配图像,但 当我刷新地图(清除注释视图并重新解析信息)时,子图层与位置和图像不匹配。 每次刷新后,子层会出现在不同的位置 - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ SYLocationItem * currentItem; static NSString *ident

对于一些注释视图,我设置了一些子层。首先加载子层匹配图像,但 当我刷新地图(清除注释视图并重新解析信息)时,子图层与位置和图像不匹配。 每次刷新后,子层会出现在不同的位置

    - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
SYLocationItem * currentItem;


static NSString *identifier = @"SYLocationItem";
if ([annotation isKindOfClass:[SYLocationItem class]]) {

     CATextLayer* subtitle1Text = [CATextLayer layer];
    CALayer *sublayer = [CALayer layer];

    MKAnnotationView *annotationView = (MKAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        annotationView.enabled = YES;
        annotationView.canShowCallout = NO;
    } else {
        annotationView.annotation = annotation;
    }

    currentItem = annotationView.annotation;



    SYJabberClient *client = [SYJabberClient sharedClient];
    [client retrieveProfileForUserWithEmail:[(SYLocationItem*)annotation email]
                           withSuccessBlock:^(NSDictionary *dict, NSError *error) {
                               if (dict) {

                                   UIImage *image = [dict objectForKey:@"imageFile"];

                                   UIImage *displayImage = [UIImage circularScaleNCrop:image
                                                                              withRect:
                                                            CGRectMake(0.0f,0.0f,30.0f,30.0f)];
                                   annotationView.image = displayImage;

                                   if(currentItem.groupSamePlace != nil){

                                       sublayer.backgroundColor = [UIColor redColor].CGColor;
                                       sublayer.cornerRadius = 9.0;
                                       sublayer.frame = CGRectMake(20, -7, 20, 20);

                                       [subtitle1Text setFontSize:12];
                                       [subtitle1Text setFrame:CGRectMake(0, 3, 20, 20)];
                                       NSString *string = [NSString stringWithFormat:@"%@", currentItem.samePlaceCount];
                                       [subtitle1Text setString:string];
                                       [subtitle1Text setAlignmentMode:kCAAlignmentCenter];
                                       [subtitle1Text setForegroundColor:[[UIColor whiteColor] CGColor]];
                                       [sublayer addSublayer:subtitle1Text];

                                       [annotationView.layer addSublayer:sublayer];
                                   }

                               }
                           }];

    return annotationView;
}

return nil;
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
SYLocationItem*当前项目;
静态NSString*标识符=@“SYLocationItem”;
if([annotation isKindOfClass:[SYLocationItem class]]){
CATextLayer*subtitle1Text=[CATextLayer层];
CALayer*子层=[CALayer层];
MKAnnotationView*annotationView=(MKAnnotationView*)[\u地图视图出列可重用annotationView with identifier:identifier];
如果(注释视图==nil){
annotationView=[[MKAnnotationView alloc]initWithAnnotation:annotation重用标识符:标识符];
annotationView.enabled=是;
annotationView.canShowCallout=否;
}否则{
annotationView.annotation=注释;
}
currentItem=annotationView.annotation;
SYJabberClient*客户端=[SYJabberClient sharedClient];
[客户端检索ProfileForUserWithEmail:[(SYLocationItem*)注释电子邮件]
withSuccessBlock:^(NSDictionary*dict,NSError*error){
if(dict){
UIImage*image=[dict objectForKey:@“imageFile”];
UIImage*displayImage=[UIImage circularScaleNCrop:image
withRect:
CGRectMake(0.0f,0.0f,30.0f,30.0f)];
annotationView.image=显示图像;
如果(currentItem.groupSamePlace!=nil){
sublayer.backgroundColor=[UIColor redColor].CGColor;
子层转角半径=9.0;
sublayer.frame=CGRectMake(20,-7,20,20);
[副标题1文本设置字体大小:12];
[副标题1文本设置帧:CGRectMake(0,3,20,20)];
NSString*string=[NSString stringWithFormat:@“%@”,currentItem.samePlaceCount];
[字幕1文本设置字符串:字符串];
[字幕1文本设置对齐模式:KCA对齐中心];
[subtitle1Text-setForegroundColor:[UIColor-whiteColor]CGColor];
[子层添加子层:subtitle1Text];
[annotationView.layer添加子层:子层];
}
}
}];
返回注释视图;
}
返回零;
}

p、 我找到了解决方法

刷新时只需不使用子句:

if (annotationView == nil){
} else {
        annotationView.annotation = annotation;
    }