0
votes

I am working on an application with an mkmapview, which drops pins onto a mapview.

I need to be able to colour the pins based on information about the pin.

The current code that drops the map pins is:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
if (annotation == self.mapView.userLocation) return nil;
NSLog(@"annotation = %@", annotation);
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* customPin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
customPin.pinColor = MKPinAnnotationColorRed;
customPin.animatesDrop = YES;
customPin.canShowCallout = YES;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
customPin.rightCalloutAccessoryView = rightButton;
return customPin;
}

If I change the line:

customPin.pinColor = MKPinAnnotationColorRed;

Then I can change the colour of ALL the dropped pins, but how can I identify which pin is being dropped, so that I can only re-colour the pin if needed?

I added the log line:

NSLog(@"annotation = %@", annotation);

But it returns, for example:

annotation = <MapAnnotation: 0x7feabd749190>
annotation = <MapAnnotation: 0x7feac04edf50>
annotation = <MapAnnotation: 0x7feabd79f860>

How can I use this to identify the pin?

Or should I be colouring the annotation pins in a different location?

1
Did you create a custom annotation class when adding annotations?? - Ahmad Ishfaq

1 Answers

0
votes

You can add any object that conforms to the MKAnnotation protocol to a map as an annotation.

I suggest creating a custom annotation object that has extra properties (like an enum for the pin type, for example)

Then in your viewForAnnotation method, once you make sure it's not the user location annotation, cast the id pointer to your custom annotation object type and check your custom properties to see what type of pin to display (it could be as simple as a switch statement.)