Несколько разных размеров аннотаций на одной карте MKMapView - PullRequest
2 голосов
/ 04 мая 2011

У меня есть приложение для 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 Ответ

1 голос
/ 04 мая 2011

Проблема в трех пропущенных else в большом условии в методе init:

// 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;
}
...