刷新后的CALayer设置为其他annotationView

时间:2014-05-19 18:32:57

标签: ios mapkit calayer mkannotationview

对于某些annotationViews,我设置了一些子图层。第一个加载子图层匹配图像,但是 当我刷新地图(清除annotationViews和重新解析信息)时,子图层与地点和图像不匹配。 每个刷新子层出现在不同的位置后。

    - (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;

}

P.S。图像在正确的位置。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

刷新时不要使用子句:

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