【问题标题】:MKMapView Off Screen Annotation Image IncorrectMKMapView 离屏注释图像不正确
【发布时间】:2025-12-04 23:30:01
【问题描述】:

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

    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 匹配。

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

【问题讨论】:

    标签: iphone ios6 mkmapview mkannotationview


    【解决方案1】:

    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

    所以你应该在设置image 的部分添加一个else 部分并将其设置为一些默认图像以防万一:

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

    【讨论】:

    • 很好的答案 ANNA 即使在很长一段时间后也能帮助我