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

女生的网名这么多〃 提交于 2019-11-30 20:48:01

问题


I have a conditional statement to add map annotation icons/pins in the method below. The problem I'm having is that the map is being populated with all the same icon. It should detect the cat id and display the icons depending on which cat id is detected. I'm not sure what the problem is because this did work in iOS 6 and now in iOS 7 the map only displays all the same annotation icon images.

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

    static NSString *defaultPinID = @"";
    annView = (MKAnnotationView *)[mapingView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( annView == nil )
        annView = [[MKAnnotationView alloc]
                   initWithAnnotation:annotation reuseIdentifier:defaultPinID] ;


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

    annView.rightCalloutAccessoryView = rightButton;

    MyAnnotation* annotation= [MyAnnotation new];

    annotation.catMapId = categoryIdNumber;
    NSLog(@"categoryIdNumber %@",categoryIdNumber);
    NSLog(@"annotation.catMapId %@",annotation.catMapId);


        if (annotation.catMapId == [NSNumber numberWithInt:9]) {
            annView.image = [UIImage imageNamed:@"PIN_comprare.png"];

            NSLog(@"annview 9");

        }

        else if (annotation.catMapId == [NSNumber numberWithInt:10]) {
            annView.image = [UIImage imageNamed:@"PIN_mangiare.png"];

            NSLog(@"annview 10");

        }

        else if (annotation.catMapId == [NSNumber numberWithInt:11]) {
            annView.image = [UIImage imageNamed:@"PIN_visitare.png"];

            NSLog(@"annview 11");

        }

        else if (annotation.catMapId == [NSNumber numberWithInt:12]) {
            annView.image = [UIImage imageNamed:@"PIN_vivere.png"];

            NSLog(@"annview 12");

        }

    annView.canShowCallout = YES;

}

return annView;

}


回答1:


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; 
}



回答2:


add this line at the end:

annView.annotation = annotation;



回答3:


Same trouble, for me the decision was not to use

pinView.animatesDrop   = YES;

custom icons just not work with animated drop for me.



来源:https://stackoverflow.com/questions/19514494/map-annotation-display-all-the-same-image-pins-for-all-points

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