I am retrieving some locations from a database and would like to customise the colour of the pins based on a category of a location. For example, all 'Favourite' locations might be purple (or even use a custom image).
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if([annotation isKindOfClass:[MKUserLocation class]]) { // This is the current location
return nil; // Don't change anything... return
}
static NSString *identifier = @"myAnnotation"; // Create a reusable class
MKPinAnnotationView * annotationView = (MKPinAnnotationView*) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annotationView) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
// .. some kind of test goes in here...
// If this is a favourite annotation, then show it in Purple
annotationView.pinColor = MKPinAnnotationColorPurple;
// Without a proper test, the current code has all pins go Red
// If it's a normal Annotation, then show it in Red
annotationView.pinColor = MKPinAnnotationColorRed;
} else {
annotationView.annotation = annotation;
}
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return annotationView;
}
What I would like is to be able to query the specific Annotation being displayed. I assume that this method is called based on what point is being shown at the time, so the order of setting the pinColor is not that predictable.
I would like to reference a specific attribute of the NSManagedObject to determine what color to set the pin. I know that I only have 3 pin colours. That's not a problem. I'd simply like to understand how this works and I can then make it more complex later.
I don't want to test on the description since it's possible for multiple descriptions to be the same but the location to be different and the category to also be different. Is it possible for example, to use a pointer within the Annotation creation to point to another class that can be referred to within the viewForAnnotation?
Any ideas?