iphone - 单个 MKMapView map 上的多个不同大小的注释

标签 iphone mkmapview mkannotationview

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

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

这是类(class)。

欢迎任何意见或建议:

#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;
}

最佳答案

问题是 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
  • 应该在 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;
}

关于iphone - 单个 MKMapView map 上的多个不同大小的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5875770/

相关文章:

ios - 可以将触摸事件转发到 MKMapView 吗?

swift - 检查 MKMarkerAnnotationView 是否在 MKMapView 中困惑或整理

swift - MKMapView 不是在 Swift 中缩小 map 时的聚类注释

iphone - 使用 AUGraph 播放麦克风的声音

iphone - 下一步按钮不显示

ios - (MapView) Userlocation image changed 帮助恢复

ios - MKMapView 放大后 MKPinAnnotationView 失去 pinColor

ios - UIButtonTypeDetailDisclosure 颜色变化

iOS:设备上的自定义位置

iphone - 使用 UIGuesture 使用 UITextview 调整 UIImageview 的大小