2

I have three arrays of annotations in my app, foodannotations, gasannotations, and shoppingannotations. I want each array of annotations to show a different colored pin. I am currently using

- (MKAnnotationView *)mapView:(MKMapView *)sheratonmap viewForAnnotation:(id<MKAnnotation>)annotation {
    NSLog(@"Welcome to the Map View Annotation");

    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    static NSString* AnnotationIdentifier = @"Annotation Identifier";
    MKPinAnnotationView* pinview = [[[MKPinAnnotationView alloc]
                                     initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];

    pinview.animatesDrop=YES;
    pinview.canShowCallout=YES;
    pinview.pinColor=MKPinAnnotationColorPurple;

    UIButton* rightbutton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightbutton setTitle:annotation.title forState:UIControlStateNormal];
    [rightbutton addTarget:self action:@selector(showDetails) forControlEvents:UIControlEventTouchUpInside];
    pinview.rightCalloutAccessoryView = rightbutton;

    return pinview;
}

How can I set this up to use the three different pin colors for each array of annotations.

Thanks!

Matt
  • 21
  • 1
  • 2

2 Answers2

6

Have you defined a custom class that implements the MKAnnotation protocol in which you added some property that identifies what kind of annotation it is? Or have you defined three separate classes (one for each type of annotation) that implement MKAnnotation?

Assuming you've defined one annotation class in which you have an int property called say annotationType in your annotation class, then you can do this in viewForAnnotation:

int annType = ((YourAnnotationClass *)annotation).annotationType;
switch (annType)
{
    case 0 :   //Food
        pinview.pinColor = MKPinAnnotationColorRed;
    case 1 :   //Gas
        pinview.pinColor = MKPinAnnotationColorGreen;
    default :  //default or Shopping
        pinview.pinColor = MKPinAnnotationColorPurple;
}


A couple of other things:

2

I would suggest you to create a custom MKAnnotation and have a custom property (most propbably a typedef enum) to distinguish between the different types of annotations.

typedef enum {
    Food,
    Gas,
    Shopping
} AnnotationType

After that you can set your colors conditionally if (annotation.annotationType == Food) { set pinColor }

Of course you can use a switch statement for the AnnotationType for having clearer code:

switch(annotation.annotationType) {
    case Food:
        do something;
        break;
    case Gas:
        do something;
        break;
    case Shopping:
        do something;
        break;
}

See following question for more info on adding more colors (if you want to extend your app later on) :

MKPinAnnotationView: Are there more than three colors available?


Here's a code snippet from a tutorial that show's heavy modifications:

calloutMapAnnotationView.contentHeight = 78.0f;
UIImage *asynchronyLogo = [UIImage imageNamed:@"asynchrony-logo-small.png"];
UIImageView *asynchronyLogoView = [[[UIImageView alloc] initWithImage:asynchronyLogo] autorelease];
asynchronyLogoView.frame = CGRectMake(5, 2, asynchronyLogoView.frame.size.width, asynchronyLogoView.frame.size.height);
[calloutMapAnnotationView.contentView addSubview:asynchronyLogoView];

HTH

Community
  • 1
  • 1
Faizan S.
  • 8,634
  • 8
  • 34
  • 63
  • is there no way that I can do this using the built in mkpinannotationview and pinview.pincolor=____? – Matt Aug 16 '11 at 20:04
  • yes, you can also use the pinColor property of your `MKAnnotationView` to do that. Also i've updated my question with a more relevant link. – Faizan S. Aug 17 '11 at 02:43
  • Thank you so much! I'm going to get to work with that right now! – Matt Aug 17 '11 at 02:56