开发者

Multiple Different Sized Annotations on Single MKMapView Map

开发者 https://www.devze.com 2023-03-02 19:59 出处:网络
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 annotat

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)ini开发者_如何学编程tWithAnnotation:(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;
}


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

精彩评论

暂无评论...
验证码 换一张
取 消