tags:

views:

539

answers:

1

Hi,

I'm trying setup a map that displays different pin colors depending on the type/class of the location in question. I know this is a pretty common thing to do, but I'm having trouble getting the viewForAnnotation delegate to consistently update/set the pin color.
I have a showThisLocation function that basically cycles through a list of AddressAnnotations and then based on the annotation class (bus stop, hospital, etc.) I set an


if( myClass == 1){
 [defaults setObject:@"1" forKey:@"currPinColor"];
 [defaults synchronize];
 NSLog(@"Should be %@!", [defaults objectForKey:@"currPinColor"]);
} else if( myClass ==2 ){
 [defaults setObject:@"2" forKey:@"currPinColor"];
 [defaults synchronize];
 NSLog(@"Should be %@!", [defaults objectForKey:@"currPinColor"]);
}
[_mapView addAnnotation:myCurrentAnnotation];

then my viewForAnnotation delegate looks like this,


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if( annotation == mapView.userLocation ){ return nil; }

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    MKPinAnnotationView *annView = nil;
    annView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"currentloc"];
    if( annView == nil ){
        annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    }

    annView.pinColor =  [defaults integerForKey:@"currPinColor"];
    NSLog(@"Pin color: %d", [defaults integerForKey:@"currPinColor"]);
    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    return annView;
}

The problem is that, although the NSLog statements in the "if" block always confirm that the color has been set, the delegate sometimes but not always ends up with the correct color. I've also noticed that what generally happens is that the first search for a new location will set all pins to the last color in the "if" block, but search for the same location again will set the pins to the correct color.

I suspect I am not supposed to usen NSUserDefaults in this way, but I also tried to create my own subclass for MKAnnotation which included an additional property "currentPinColor", and while this allowed me to set the "currentPinColor", when I tried to access the "currentPinColor from the delegate method, the compiler complained that it didn't know anything about "currentPinColor in connection with MKAnnotation. Fair enough I guess, but then I tried to revise the delegate method,


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MyCustomMKAnnotation>)annotation 

instead of the default


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 

at which point the compiler complained that it didn't know anything about the protocol for MyCustomMKAnnotation in this delegate context.

What is the proper way to set the delegate method and/or MyCustomMKAnnotation, or what is the appropriate way to achieve consistent pinColor settings. I'm just about out of ideas for things to try here.

+2  A: 

I solved my own problem. Sometimes just formulating the question is enough! The issue was the way I was trying to access my MKAnnotation delegate.

After posting I happened on these two related posts,

http://stackoverflow.com/questions/1185611/mkpinannotationview-are-there-more-than-three-colors-available

http://stackoverflow.com/questions/2405233/mapkit-issue-in-finding-annotation-current-position

The answer to the first post, by user Cannonade, suggests


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{   
    // ... get the annotation delegate and allocate the MKAnnotationView (annView)
    if ([annotationDelegate.type localizedCaseInsensitiveCompare:@"NeedsBluePin"] == NSOrderedSame)
    {
        UIImage * image = [UIImage imageNamed:@"blue_pin.png"];
        UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
        [annView addSubview:imageView];
    }
    // ...

however my issue was that I didn't know how to the get the delegate and it was floating around somewhere in the ... ellipsis part of the above snippet. The answer to the second post, also by user Cannonade, filled this missing bit in,


- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    AnnotationDelegate * delegate = (AnnotationDelegate*)view.annotation;
    // do stuff with delegate.position;
}

Putting these two snippets together produced the desired result and finally solved my problem.

The working viewForAnnotation delegate method then looks like,


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if( annotation == mapView.userLocation ){ return nil; }
    AddressAnnotation *delegate = annotation;  //THIS CAST WAS WHAT WAS MISSING!
    MKPinAnnotationView *annView = nil;
    annView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"currentloc"];
    if( annView == nil ){
        annView = [[MKPinAnnotationView alloc] initWithAnnotation:delegate reuseIdentifier:@"currentloc"];
    }

    if ([delegate.classPinColor localizedCaseInsensitiveCompare:@"green"] == NSOrderedSame) {
        annView.pinColor = MKPinAnnotationColorGreen;
    }else if ([delegate.classPinColor localizedCaseInsensitiveCompare:@"purple"] == NSOrderedSame){
        annView.pinColor = MKPinAnnotationColorPurple;
    }else if ([delegate.classPinColor localizedCaseInsensitiveCompare:@"red"] == NSOrderedSame){
        annView.pinColor = MKPinAnnotationColorRed;
    }
    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    return annView;
}

Hopefully this will be of help to someone else.

Thanks Cannonade!

blackkettle
why does it take 2 days to answer my own question?
blackkettle