映射注释未正确显示附件视图

时间:2013-06-19 19:55:00

标签: objective-c mkmapview mkannotationview

我正在向MKMapView添加一些注释,这些注释有一个UIImageView,与AFNetworking异步加载,作为左标注附件视图。这是实施:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    static NSString *identifier = @"MapAnnotation";

    if ([annotation isKindOfClass:[MapAnnotation class]]) {

        MKAnnotationView *annotationView = (MKAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            annotationView.enabled = YES;
            annotationView.canShowCallout = YES;
            annotationView.image = [UIImage imageNamed:@"map_pin"];

            UIImageView *userAvatar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
            [userAvatar setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=small",[[(MapAnnotation *)annotation user] facebookID]]] placeholderImage:[UIImage imageNamed:@"user_placeholder"]];
            userAvatar.layer.cornerRadius = 4.0;
            userAvatar.layer.masksToBounds = YES;
            userAvatar.layer.borderColor = [[UIColor blackColor] CGColor];
            userAvatar.layer.borderWidth = 1;

            annotationView.leftCalloutAccessoryView = userAvatar;

            UIButton *rightCallout = [UIButton buttonWithType:UIButtonTypeInfoLight];

            annotationView.rightCalloutAccessoryView = rightCallout;

        }

        annotationView.annotation = annotation;

        return annotationView;
    }

    return nil;
}

这里的问题是:有时(并非所有时间)将不同用户的注释加载到地图中时,它们会为所有用户显示相同的图像。

假设我必须使用以下信息加载两个注释:

注释1 姓名(标题):约书亚 日期时间(副标题):19 / 06,11:00 图片:www.myurl.com/joshua.jpg

注释2 姓名(职称):马修 日期时间(副标题):10 / 06,20:00 图片:www.myurl.com/mathew.jpg

有时它们会正确显示标题和副标题,但加载的图像对所有图像都是相同的(约书亚的图像,反之亦然)。

我错过了这个方法吗?

1 个答案:

答案 0 :(得分:1)

dequeueReusableAnnotationViewWithIdentifier:的调用将在第一次调用返回nil,这将满足annotationView == nil并设置注释视图。因此,对dequeueReusableAnnotationViewWithIdentifier:的所有以下调用都将返回带有指定头像图像的第一个创建的注释视图。

所以[userAvatar setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=small",[[(MapAnnotation *)annotation user] facebookID]]] placeholderImage:[UIImage imageNamed:@"user_placeholder"]];只被调用一次。

您需要将注释特定更改移动到“if nil”设置范围之外的注释视图。

因此,每当mapview请求annotationView时,您应该设置userAvatar图像。 所以像这样:

MKAnnotationView *annotationView = (MKAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            annotationView.enabled = YES;
            annotationView.canShowCallout = YES;
            annotationView.image = [UIImage imageNamed:@"map_pin"];

            UIImageView *userAvatar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
            userAvatar.layer.cornerRadius = 4.0;
            userAvatar.layer.masksToBounds = YES;
            userAvatar.layer.borderColor = [[UIColor blackColor] CGColor];
            userAvatar.layer.borderWidth = 1;

            annotationView.leftCalloutAccessoryView = userAvatar;

            UIButton *rightCallout = [UIButton buttonWithType:UIButtonTypeInfoLight];

            annotationView.rightCalloutAccessoryView = rightCallout;
        }
[((UIImageView *)annotationView.leftCalloutAccessoryView) setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=small",[[(MapAnnotation *)annotation user] facebookID]]] placeholderImage:[UIImage imageNamed:@"user_placeholder"]];

每次地图视图要求注释视图时,这将分配正确的图像。

干杯 的Morten

相关问题