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

时间:2011-05-03 21:12:42

标签: iphone mkmapview mkannotationview

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

1 个答案:

答案 0 :(得分:1)

问题是在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"];
    }

<小时/> 两个独立的,无关的问题是:

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

因此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;
}