-1

I have a bunch of pins on my map, some red and some green. The initial coloring is ok. However, when I use the map view and tap on some green ones, some reds will change to green. That probably happens when they are outside of the current view area and moved into the view area. Ideas anyone?

Here's my code snip from :

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

    if(((MyAnnotation*)annotation).isGreen){
        AnnotationViewID = @"MyAnnotationGreen";
    }else{
        AnnotationViewID = @"MyAnnotationRed";
    }

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil)
    {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
    }
El Dude
  • 5,328
  • 11
  • 54
  • 101

1 Answers1

0

You should use the isGreen property of your MyAnnotation object to assign the color to your MKPinAnnotationView object. The reuse code could give you an object that doesn't have all the properties set right, especially since you are using the exact same class under different identifier. You shouldn't rely on the validity of all property values of a dequeue object from view caches (such as uitableview dequeue).

do something like:

MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

if (annotationView == nil)
{
    annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
}
[annotationView setPinColor:(annotation.isGreen)? MKPinAnnotationColorGreen : MKPinAnnotationColorRed];
J2theC
  • 4,412
  • 1
  • 12
  • 14