Map annotation display all the same image/pins for all points

不打扰是莪最后的温柔 提交于 2019-12-01 01:32:32

If, as you say, "this did work in iOS 6", you should consider it quite fortunate that it did (or seemed to) and this approach of setting the annotation's image should not be relied on under any version.

Although @Ar Ma is correct that the annotation view's annotation property should be set (in case the view is being re-used), that won't solve the main issue.

The annotation view's image is set based on the value of categoryIdNumber which seems to be some variable outside the viewForAnnotation delegate method.

You cannot assume that:

  1. viewForAnnotation will be called immediately after you call addAnnotation. Even in iOS 6 or earlier, this is not guaranteed.
  2. viewForAnnotation will be called only once for each annotation. The delegate method can be called multiple times for the same annotation as the user pans or zooms the map and the annotation comes back onto the screen.
  3. viewForAnnotation will be called in the same order that you add the annotations. This is a result of points 1 and 2.

I assume that just before you call addAnnotation, the categoryIdNumber is set correctly and then based on the above incorrect assumptions, viewForAnnotation uses categoryIdNumber to set the image.

What is happening is that viewForAnnotation is being called by the map view sometime after all or some of the addAnnotation calls are done at which point categoryIdNumber is probably the value connected with the last annotation added and all the annotations use the image applicable to the last annotation.


To fix this (regardless of the iOS version), you must put the correct categoryIdNumber value into each annotation object before calling addAnnotation.

It looks like your annotation class is MyAnnotation and you already have a catMapId property in it.

You must set this property in the annotation before calling addAnnotation -- not inside the viewForAnnotation method which is too late. (By the way, you are creating a MyAnnotation object inside the viewForAnnotation method which is pointless.)


So where you create and add the annotations (not in viewForAnnotation):

MyAnnotation* myAnn = [[MyAnnotation alloc] init];
myAnn.coordinate = ...
myAnn.title = ...
myAnn.catMapId = categoryIdNumber;  // <-- set catMapId BEFORE addAnnotation
[mapView addAnnotation:myAnn];

Then the code in viewForAnnotation should be like this:

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

        static NSString *defaultPinID = @"MyAnnId";
        annView = (MKAnnotationView *)[mapingView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( annView == nil )
        {
            annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] ;
            annView.canShowCallout = YES;
        }
        else
        {
            //view is being re-used, re-set annotation to current...
            annView.annotation = annotation;
        }

        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton setTitle:annotation.title forState:UIControlStateNormal];

        annView.rightCalloutAccessoryView = rightButton;


        //Make sure we have a MyAnnotation-type annotation
        if ([annotation isKindOfClass:[MyAnnotation class]])
        {
            //Do not CREATE a local MyAnnotation object here.
            //Instead, get the catMapId from the annotation object
            //that was PASSED INTO the delegate method.
            //MyAnnotation* annotation= [MyAnnotation new];
            //annotation.catMapId = categoryIdNumber;

            MyAnnotation *myAnn = (MyAnnotation *)annotation;

            //The value of the external variable categoryIdNumber is irrelevant here.
            //NSLog(@"categoryIdNumber %@",categoryIdNumber);

            NSLog(@"myAnn.catMapId %@",myAnn.catMapId);


            //Put the NSNumber value into an int to simplify the code below.
            int myAnnCatMapId = [myAnn.catMapId intValue];

            NSString *imageName = nil;
            switch (myAnnCatMapId)
            {
                case 9:
                {
                    imageName = @"PIN_comprare.png";
                    break;
                }

                case 10:
                {
                    imageName = @"PIN_mangiare.png";
                    break;
                }

                case 11:
                {
                    imageName = @"PIN_mangiare.png";
                    break;
                }

                case 12:
                {
                    imageName = @"PIN_vivere.png";
                    break;
                }

                default:
                {
                    //set some default image for unknown cat ids...
                    imageName = @"default.png";
                    break;
                }
            }

            annView.image = [UIImage imageNamed:imageName];

            NSLog(@"annview %d", myAnnCatMapId);
        }
    }

    return annView; 
}

add this line at the end:

annView.annotation = annotation;

Same trouble, for me the decision was not to use

pinView.animatesDrop   = YES;

custom icons just not work with animated drop for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!