我有一个 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;
}