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)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
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:Two separate, unrelated issues are:
viewForAnnotationbecause it is not releasingaViewdequeueReusableAnnotationViewWithIdentifierinviewForAnnotationSo the
viewForAnnotationmethod should look like this: