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 )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 )annotation
instead of the default
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id )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.