RaYell
RaYell

Reputation: 70454

Several pin colors on same map in MKMapView

I have a MKMapView in my app with several pins on it and I'd like to set different colors for each pin. My view controller is implementing MKMapViewDelegate and I've defined viewForAnnotation method.

- (MKAnnotationView *) mapView:(MKMapView *)mapView 
viewForAnnotation:(id <MKAnnotation>) annotation {
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] 
        initWithAnnotation:annotation reuseIdentifier:@"pin"];
    annView.pinColor = MKPinAnnotationColorGreen;
    return annView;
}

It works fine and changes pin color to green. However the color is changed for all pins and I'd like to color them with several colors (based on some criteria I'd define, lets assume I want to have odd pins green and even pins yellow or something as simple as that). How can this be achieved?

Upvotes: 2

Views: 7637

Answers (3)

SURESH SANKE
SURESH SANKE

Reputation: 1663

I have met the same issue then I solved by using this code

if([[pinView.annotation title] isEqualToString:@"Current Location"])
{
    pinView.pinColor = MKPinAnnotationColorRed; 
}
else
{
    pinView.pinColor = MKPinAnnotationColorPurple; 
}

Upvotes: 0

Gubb
Gubb

Reputation: 421

if(annotation.fillsYourCriteria)  
    annView.pinColor = MKPinAnnotationColorGreen;  
else  
    annView.pinColor = MKPinAnnotationColorYellow;  
return annView;  

Something as simple as that?

Upvotes: 0

RaYell
RaYell

Reputation: 70454

I've solved this issue by using images instead of pinColor. This way I can have as many pins as I want.

Upvotes: 2

Related Questions