1

我有一张地图,我在其中添加了几个注释,如下所示:

    for (Users *user in mapUsers){

        double userlat = [user.llat doubleValue];
        double userLong = [user.llong doubleValue];

        CLLocationCoordinate2D userCoord = {.latitude =  userlat, .longitude =  userLong};

        MapAnnotationViewController *addAnnotation = [[MapAnnotationViewController alloc] initWithCoordinate:userCoord];

        NSString *userName = user.username;
        NSString *relationship = user.relationship;

        [addAnnotation setTitle:userName];
        [addAnnotation setRelationshipParam:relationship];

        [self.mainMapView addAnnotation:addAnnotation];
    }

使用此委托方法代码:

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

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

    MKAnnotationView *annView = (MKAnnotationView *)[self.mainMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (!annView) {
        annView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                          reuseIdentifier:identifier];
    }
    MapAnnotationViewController *sac = (MapAnnotationViewController *)annView.annotation;

    if ([sac.relationshipParam isEqualToString:@"paramA"])
    {
        annView.image = [UIImage imageNamed:@"image1.png"];
    }
    else if ([sac.relationshipParam isEqualToString:@"paramB"])
    {
        annView.image = [UIImage imageNamed:@"image2.png"];
    }
    else if ([sac.relationshipParam isEqualToString:@"paramC"])
    {
        annView.image = [UIImage imageNamed:@"image3.png"];
    }


    return annView;
}
else {
    return nil;
}

这一切都适用于地图的原始加载。但是,当我选择注释(其自定义代码太长而无法发布但包含放大)时,之前绘制的注释图像已更改图标。在该过程中不会重新绘制地图并且不会重新添加注释。当我在地图上向后捏时,图像是不同的(它们将错误的关系参数与错误的 image1-3.png 匹配。

谁能想到为什么会发生这种情况,或者要寻找什么?

4

1 回答 1

6

可能会返回一个注释视图,该dequeueReusableAnnotationViewWithIdentifier视图用于与当前annotation参数不同的注释。

如果dequeueReusableAnnotationViewWithIdentifier成功(即您正在使用以前使用的注释视图),则必须更新其annotation属性以确保视图与当前annotation的属性匹配。

所以尝试改变这部分:

MKAnnotationView *annView = (MKAnnotationView *)[self.mainMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annView) {
    annView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                      reuseIdentifier:identifier];
}
MapAnnotationViewController *sac = (MapAnnotationViewController *)annView.annotation;

到:

MKAnnotationView *annView = (MKAnnotationView *)[self.mainMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annView) {
    annView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                      reuseIdentifier:identifier];
}
else {
    annView.annotation = annotation; // <-- add this
}

MapAnnotationViewController *sac = (MapAnnotationViewController *)annView.annotation;


另一个潜在问题(不会导致问题中的问题)是视图的属性仅在是三个值之一 image时才设置。relationshipParam

如果不知何故relationshipParam不是这三个编码值之一并且视图已出队,则图像将基于其他一些注释的relationshipParam.

因此,您应该在设置部分添加一个else部分image并将其设置为一些默认图像,以防万一:

...
else if ([sac.relationshipParam isEqualToString:@"paramC"])
{
    annView.image = [UIImage imageNamed:@"image3.png"];
}
else
{
    annView.image = [UIImage imageNamed:@"UnknownRelationship.png"];
}
于 2012-12-13T22:59:12.517 回答