views:

736

answers:

1

I have three pins on a mapview. I'd like to give each one a different color. In the delegate method viewForAnnotation, I'm doing this:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"anAddress"];
annView.pinColor = MKPinAnnotationColorGreen;
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
}

I was thinking to create an array of MKPinAnnotationViews but how can I get the correct one since the delegate method isn't indexed to anything?

+3  A: 

To distinguish between UIView, you can use the tag property and its corresponding viewWithTag: method.

However, in your context, I would recommend adding the color to your annotation class. Then you can ensure that you don't reuse the same caller for multiple pins.

notnoop
You mean by making a custom MKPinAnnotationView? But still, how do I call the correct one when the delegate fires? Or does it matter? If not, I can just keep a class level counter to pull them out of an array upon each firing of the delegate.
4thSpace
I mean that your id<MKAnnotation> class contains a field for the color indicator, or a counter you use to determine the color.
notnoop
Perfect! Thanks.
4thSpace