The map annotation displays all the same images / contacts for all points

I have a conditional instruction for adding annotation icons / pictograms in the method below. The problem I am facing is that the same icon is filling on the map. It should detect the cat identifier and display icons depending on which cat identifier is detected. I'm not sure what the problem is, because it works in iOS 6, and now in iOS 7 only the same annotation icon images are displayed on the map.

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

}

enter image description here

+1
source share
3 answers

, , " iOS 6", , ( ), .

@Ar Ma , annotation ( ), .

image categoryIdNumber, , , viewForAnnotation.

, :

  • viewForAnnotation addAnnotation. iOS 6 .
  • viewForAnnotation . , , .
  • viewForAnnotation , . 1 2.

, , addAnnotation, categoryIdNumber , , , viewForAnnotation categoryIdNumber .

, , viewForAnnotation , addAnnotation , categoryIdNumber, , , , , .


( iOS), categoryIdNumber , addAnnotation.

, MyAnnotation, catMapId.

, addAnnotation - viewForAnnotation, . (, MyAnnotation viewForAnnotation, .)


, ( viewForAnnotation):

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

viewForAnnotation :

- (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; 
}
+1

:

annView.annotation = annotation;
+1

,

pinView.animatesDrop   = YES;

.

0

Source: https://habr.com/ru/post/1547438/


All Articles