0

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?

Jon M
  • 530
  • 5
  • 17
  • 1
    If you aren't already, use a custom class that conforms to MKAnnotation and add a property in there that identifies it as "favorite" or not (or a reference to some other object that has that property). See http://stackoverflow.com/questions/10898122/map-view-annotations-with-different-pin-colors, http://stackoverflow.com/questions/24215210/does-mkannotationview-buffer-its-input-queue. In viewForAnnotation, the important thing is to do the test _after_ the if/else (not just when creating the view). –  Aug 27 '14 at 10:57
  • Thanks - I'll have a go and get back once I've done it. – Jon M Aug 27 '14 at 23:51
  • Thanks Anna - I've been looking at a load of code, and I see your logic. However, I'm unsure whether to create a custom class from MKAnnotation, MKPointAnnotation, or MKAnnotationView. When I create the Annotation Point, it is of an MKPointAnnotation. I've subclassed all of them, but am not clear when to use which. I get the feeling this is a subclass of MKPointAnnotation, but I don't know how to reference it within the viewForAnnotation method. Looking at other examples doesn't seem to get me closer. – Jon M Aug 29 '14 at 20:46
  • 1
    The objects that you add to the map (which you give to the addAnnotation method) should conform to MKAnnotation (which is a protocol not a class) or be a subclass of MKPointAnnotation. To reference it in viewForAnnotation, you cast the `annotation` parameter to your custom class (eg. `(MyCustomClass *)annotation`). The links I gave in the first comment show examples of that. –  Aug 29 '14 at 21:06
  • 1
    Also see: http://stackoverflow.com/questions/5939223/store-data-in-mkannotation, http://stackoverflow.com/questions/7935642/should-i-use-mkannotation-mkannotationview-or-mkpinannotation, http://stackoverflow.com/questions/4713821/mkmapview-animatedrop –  Aug 29 '14 at 21:06
  • Thanks Anna - sorted. I casted the annotation parameter as you suggest and it all worked. – Jon M Aug 30 '14 at 13:35

0 Answers0