单个 MKMapView 地图上的多个不同大小的注释

发布于 2024-11-04 21:07:55 字数 2810 浏览 0 评论 0原文

我有一个 iPhone 应用程序,可以在地图上向用户显示位置,目标是标记的大小与位置的受欢迎程度成正比。更受欢迎的地方会得到稍大的注释。

我有一个自定义 MKAnnotation 和 MKAnnotationView 来显示标记。我尝试使用自定义 MKAnnotationView 来渲染不同大小的标记,但始终渲染相同大小的图像。

这是班级。

欢迎任何意见或建议:

#import "MapAnnotationView.h"

@implementation MapAnnotationView

- (id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
MapAnnotation * myAnnotation = (MapAnnotation *) annotation;
self = [super initWithAnnotation:myAnnotation reuseIdentifier:reuseIdentifier];

// Figure out if in 'normal' mode or 'compare' mode
// normal
if (myAnnotation.visited != nil) {
    if ([myAnnotation.visited boolValue] == YES) {
        self.image = [UIImage imageNamed:@"visited.png"];
    } else if ([myAnnotation.visited boolValue] == NO) {
        self.image = [UIImage imageNamed:@"unvisited.png"];
    }
// compare
} else {

    // On both maps
    if ([myAnnotation.onLoggedInUsersMap boolValue] == YES && [myAnnotation.onComparisonMap boolValue] == YES) {
        if ([myAnnotation.mapCount intValue] == 1) {
            self.image = [UIImage imageNamed:@"both_small.png"];
        } if ([myAnnotation.mapCount intValue] < 5) {
            self.image = [UIImage imageNamed:@"both_medium.png"];
        } else {
            self.image = [UIImage imageNamed:@"both_large.png"];
        }
    // Only on comparison's map
    } else if ([myAnnotation.onLoggedInUsersMap boolValue] == NO && [myAnnotation.onComparisonMap boolValue] == YES) {
        if ([myAnnotation.mapCount intValue] == 1) {
            self.image = [UIImage imageNamed:@"compare_small.png"];
        } if ([myAnnotation.mapCount intValue] < 5) {
            self.image = [UIImage imageNamed:@"compare_medium.png"];
        } else {
            self.image = [UIImage imageNamed:@"compare_large.png"];
        }
        // Only on owner's map
    } else {
        if ([myAnnotation.mapCount intValue] == 1) {
            self.image = [UIImage imageNamed:@"owner_small.png"];
        } if ([myAnnotation.mapCount intValue] < 5) {
            self.image = [UIImage imageNamed:@"owner_medium.png"];
        } else {
            self.image = [UIImage imageNamed:@"owner_large.png"];
        }
    }
}
return self;
}

@end

这是 viewForAnnotation 方法:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
    if ([annotation class] == MKUserLocation.class) {
        return nil;
    }

    MapAnnotationView *aView = [[MapAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"location"];
    [aView setEnabled:YES];
    [aView setCanShowCallout:YES];
    aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    return aView;
}

I've an iPhone app that shows users locations on a map and the goal is that the size of the marker is proportional to the popularity of the location. More popular places get a slightly larger annotation.

I've a custom MKAnnotation and MKAnnotationView to display markers. I've tried to use the custom MKAnnotationView to render the different sized markers, but the same sized images are always rendered.

Here's the class.

Any advice or suggestions are welcome:

#import "MapAnnotationView.h"

@implementation MapAnnotationView

- (id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
MapAnnotation * myAnnotation = (MapAnnotation *) annotation;
self = [super initWithAnnotation:myAnnotation reuseIdentifier:reuseIdentifier];

// Figure out if in 'normal' mode or 'compare' mode
// normal
if (myAnnotation.visited != nil) {
    if ([myAnnotation.visited boolValue] == YES) {
        self.image = [UIImage imageNamed:@"visited.png"];
    } else if ([myAnnotation.visited boolValue] == NO) {
        self.image = [UIImage imageNamed:@"unvisited.png"];
    }
// compare
} else {

    // On both maps
    if ([myAnnotation.onLoggedInUsersMap boolValue] == YES && [myAnnotation.onComparisonMap boolValue] == YES) {
        if ([myAnnotation.mapCount intValue] == 1) {
            self.image = [UIImage imageNamed:@"both_small.png"];
        } if ([myAnnotation.mapCount intValue] < 5) {
            self.image = [UIImage imageNamed:@"both_medium.png"];
        } else {
            self.image = [UIImage imageNamed:@"both_large.png"];
        }
    // Only on comparison's map
    } else if ([myAnnotation.onLoggedInUsersMap boolValue] == NO && [myAnnotation.onComparisonMap boolValue] == YES) {
        if ([myAnnotation.mapCount intValue] == 1) {
            self.image = [UIImage imageNamed:@"compare_small.png"];
        } if ([myAnnotation.mapCount intValue] < 5) {
            self.image = [UIImage imageNamed:@"compare_medium.png"];
        } else {
            self.image = [UIImage imageNamed:@"compare_large.png"];
        }
        // Only on owner's map
    } else {
        if ([myAnnotation.mapCount intValue] == 1) {
            self.image = [UIImage imageNamed:@"owner_small.png"];
        } if ([myAnnotation.mapCount intValue] < 5) {
            self.image = [UIImage imageNamed:@"owner_medium.png"];
        } else {
            self.image = [UIImage imageNamed:@"owner_large.png"];
        }
    }
}
return self;
}

@end

And here's the viewForAnnotation method:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
    if ([annotation class] == MKUserLocation.class) {
        return nil;
    }

    MapAnnotationView *aView = [[MapAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"location"];
    [aView setEnabled:YES];
    [aView setCanShowCallout:YES];
    aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    return aView;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

塔塔猫 2024-11-11 21:07:55

问题是 init 方法的大条件中缺少三个 else

// On both maps
if ([myAnnotation.onLoggedInUsersMap boolValue] == YES && [myAnnotation.onComparisonMap boolValue] == YES) {
    if ([myAnnotation.mapCount intValue] == 1) {
        self.image = [UIImage imageNamed:@"both_small.png"];
    } else if ([myAnnotation.mapCount intValue] < 5) {  // <----------------
      // ^-- was missing else before the if
        self.image = [UIImage imageNamed:@"both_medium.png"];
    } else {
        self.image = [UIImage imageNamed:@"both_large.png"];
    }
    // Only on comparison's map
} else if ([myAnnotation.onLoggedInUsersMap boolValue] == NO && [myAnnotation.onComparisonMap boolValue] == YES) {
    if ([myAnnotation.mapCount intValue] == 1) {
        self.image = [UIImage imageNamed:@"compare_small.png"];
    } else if ([myAnnotation.mapCount intValue] < 5) {  // <----------------
      // ^-- was missing else before the if
        self.image = [UIImage imageNamed:@"compare_medium.png"];
    } else {
        self.image = [UIImage imageNamed:@"compare_large.png"];
    }
    // Only on owner's map
} else {
    if ([myAnnotation.mapCount intValue] == 1) {
        self.image = [UIImage imageNamed:@"owner_small.png"];
    } else if ([myAnnotation.mapCount intValue] < 5) {  // <----------------
      // ^-- was missing else before the if
        self.image = [UIImage imageNamed:@"owner_medium.png"];
    } else {
        self.image = [UIImage imageNamed:@"owner_large.png"];
    }


Two separate, unrelated issues are:

  • viewForAnnotation 中的内存泄漏,因为它没有释放 aView
  • 应该在 viewForAnnotation 中使用 dequeueReusableAnnotationViewWithIdentifier

所以 viewForAnnotation 方法应该如下所示:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
    if ([annotation class] == MKUserLocation.class) {
        return nil;
    }

    MapAnnotationView *aView = (MapAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"location"];
    if (!aView)
    {
        aView = [[[MapAnnotationView alloc] initWithAnnotation:annotation 
                      reuseIdentifier:@"location"] autorelease];
        [aView setEnabled:YES];
        [aView setCanShowCallout:YES];
        aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    }
    else
    {
        aView.annotation = annotation;
    }

    return aView;
}

The problem is three missing else's in the big conditional in the init method:

// On both maps
if ([myAnnotation.onLoggedInUsersMap boolValue] == YES && [myAnnotation.onComparisonMap boolValue] == YES) {
    if ([myAnnotation.mapCount intValue] == 1) {
        self.image = [UIImage imageNamed:@"both_small.png"];
    } else if ([myAnnotation.mapCount intValue] < 5) {  // <----------------
      // ^-- was missing else before the if
        self.image = [UIImage imageNamed:@"both_medium.png"];
    } else {
        self.image = [UIImage imageNamed:@"both_large.png"];
    }
    // Only on comparison's map
} else if ([myAnnotation.onLoggedInUsersMap boolValue] == NO && [myAnnotation.onComparisonMap boolValue] == YES) {
    if ([myAnnotation.mapCount intValue] == 1) {
        self.image = [UIImage imageNamed:@"compare_small.png"];
    } else if ([myAnnotation.mapCount intValue] < 5) {  // <----------------
      // ^-- was missing else before the if
        self.image = [UIImage imageNamed:@"compare_medium.png"];
    } else {
        self.image = [UIImage imageNamed:@"compare_large.png"];
    }
    // Only on owner's map
} else {
    if ([myAnnotation.mapCount intValue] == 1) {
        self.image = [UIImage imageNamed:@"owner_small.png"];
    } else if ([myAnnotation.mapCount intValue] < 5) {  // <----------------
      // ^-- was missing else before the if
        self.image = [UIImage imageNamed:@"owner_medium.png"];
    } else {
        self.image = [UIImage imageNamed:@"owner_large.png"];
    }


Two separate, unrelated issues are:

  • memory leak in viewForAnnotation because it is not releasing aView
  • should be using dequeueReusableAnnotationViewWithIdentifier in viewForAnnotation

So the viewForAnnotation method should look like this:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
    if ([annotation class] == MKUserLocation.class) {
        return nil;
    }

    MapAnnotationView *aView = (MapAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"location"];
    if (!aView)
    {
        aView = [[[MapAnnotationView alloc] initWithAnnotation:annotation 
                      reuseIdentifier:@"location"] autorelease];
        [aView setEnabled:YES];
        [aView setCanShowCallout:YES];
        aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    }
    else
    {
        aView.annotation = annotation;
    }

    return aView;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文