【问题标题】:Pin colors not showed as expected as a regular basis引脚颜色未按预期正常显示
【发布时间】:2014-03-18 23:47:19
【问题描述】:

在回答我的问题Detecting selected annotation to change pin color 的第一部分之后,我更新了我的代码如下:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
    //7
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    //8
    static NSString *identifier = @"myAnnotation";
    MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (!annotationView)
    {
        //9
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        {
            myAnnotation *myAnn = (myAnnotation *)annotation;
            NSLog(@"ANNOTATION GRUPO = %d",myAnn.grupo);
            switch (myAnn.grupo)
            {
                case 1:
                    annotationView.pinColor = MKPinAnnotationColorRed;
                    break;
                case 2:
                    annotationView.pinColor = MKPinAnnotationColorGreen;
                    break;
                case 3:
                    annotationView.pinColor = MKPinAnnotationColorPurple;
                    break;
                default:
                    annotationView.pinColor = MKPinAnnotationColorPurple;
                    break;
            }
        }
        annotationView.animatesDrop = YES;
        annotationView.canShowCallout = YES;

    }else {
        annotationView.annotation = annotation;
    }
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    return annotationView;
}

而changeOpcion方法如下:

- (IBAction)changeOpcion:(id)sender{

    if(miControl.selectedSegmentIndex == 0)
    {
        //[mapView_ clear];

        [self removeAllPinsButUserLocation2];
        NSLog(@"********0");
        for ( int i=0;i<[categorias count];i++){

            int grupo = [[[categorias objectAtIndex:i] objectForKey:@"procedencia"] integerValue];



            if (grupo == 1){

                double latitud = [[[categorias objectAtIndex:i] objectForKey:@"latitud"] doubleValue];

                double longitud = [[[categorias objectAtIndex:i] objectForKey:@"longitud"]doubleValue];

                CLLocationCoordinate2D lugar;
                lugar.latitude = latitud;
                lugar.longitude = longitud;

                NSString *nombre = [[categorias objectAtIndex:i] objectForKey:@"titulo"];


                NSString *direccion = [[categorias objectAtIndex:i] objectForKey:@"id"];

                CLLocationCoordinate2D coordinate3;
                coordinate3.latitude = latitud;
                coordinate3.longitude = longitud;
                myAnnotation *annotation3 = [[myAnnotation alloc] initWithCoordinate:coordinate3 title:nombre subtitle:direccion];

                annotation3.grupo = 1;
                [self.mapView addAnnotation:annotation3];

            }
        }
    }
    else if(miControl.selectedSegmentIndex == 1)
    {
        [self removeAllPinsButUserLocation2];

        for ( int i=0;i<[categorias count];i++){

            int grupo = [[[categorias objectAtIndex:i] objectForKey:@"procedencia"] integerValue];



            if (grupo == 2){

                double latitud = [[[categorias objectAtIndex:i] objectForKey:@"latitud"] doubleValue];

                double longitud = [[[categorias objectAtIndex:i] objectForKey:@"longitud"]doubleValue];

                CLLocationCoordinate2D lugar;
                lugar.latitude = latitud;
                lugar.longitude = longitud;

                NSString *nombre = [[categorias objectAtIndex:i] objectForKey:@"titulo"];


                NSString *direccion = [[categorias objectAtIndex:i] objectForKey:@"direccion"];


                CLLocationCoordinate2D coordinate3;
                coordinate3.latitude = latitud;
                coordinate3.longitude = longitud;
                myAnnotation *annotation3 = [[myAnnotation alloc] initWithCoordinate:coordinate3 title:nombre subtitle:direccion];

                annotation3.grupo = 2;
                [self.mapView addAnnotation:annotation3];


            }
        }

        //action for the second button
    }
    else if(miControl.selectedSegmentIndex == 2)
    {
        [self removeAllPinsButUserLocation2];

        for ( int i=0;i<[categorias count];i++){

            int grupo = [[[categorias objectAtIndex:i] objectForKey:@"procedencia"] integerValue];



            if (grupo == 3){

                double latitud = [[[categorias objectAtIndex:i] objectForKey:@"latitud"] doubleValue];

                double longitud = [[[categorias objectAtIndex:i] objectForKey:@"longitud"]doubleValue];

                CLLocationCoordinate2D lugar;
                lugar.latitude = latitud;
                lugar.longitude = longitud;

                NSString *nombre = [[categorias objectAtIndex:i] objectForKey:@"titulo"];


                NSString *direccion = [[categorias objectAtIndex:i] objectForKey:@"direccion"];


                CLLocationCoordinate2D coordinate3;
                coordinate3.latitude = latitud;
                coordinate3.longitude = longitud;
                myAnnotation *annotation3 = [[myAnnotation alloc] initWithCoordinate:coordinate3 title:nombre subtitle:direccion];
                annotation3.grupo = 3;
                [self.mapView addAnnotation:annotation3];


            }
        }
    }


}

当用户从 segmentedIndex 控件中选择选项 #2 (Eventos) 时,引脚为紫色,如预期的那样。然后,如果用户选择选项 #1 (Cursos),则引脚为绿色,如预期的那样,如果用户选择选项 #0 (Ofertas),则引脚为红色,如预期的那样,但之后,如果用户选择任何选项,所有引脚都是红色的。为什么???

【问题讨论】:

    标签: ios annotations mapkit mkpinannotationview


    【解决方案1】:

    听起来图钉视图正在从以前的注释中重复使用,并且它们的pinColor 在重复使用时没有更新。

    在您链接到的答案中,有这样的声明:

    最后,在 viewForAnnotation 中,在返回视图之前,根据 grupo 设置 pinColor:

    pinColor 需要设置,无论您是创建新视图还是,如果您要重新使用已出队的视图。出队视图可能已将其 pinColor 设置为不同组中的注释。

    问题中显示的代码只是在创建新视图时设置pinColor

    需要将该代码移出该块并放在return annotationView; 行之前:

    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    
    //the code to set the pinColor goes HERE...
    myAnnotation *myAnn = (myAnnotation *)annotation;
    NSLog(@"ANNOTATION GRUPO = %d",myAnn.grupo);
    switch (myAnn.grupo)
    {
        case 1:
            annotationView.pinColor = MKPinAnnotationColorRed;
            break;
        case 2:
            annotationView.pinColor = MKPinAnnotationColorGreen;
            break;
        case 3:
            annotationView.pinColor = MKPinAnnotationColorPurple;
            break;
        default:
            annotationView.pinColor = MKPinAnnotationColorPurple;
            break;
    }
    
    return annotationView;
    

    【讨论】:

    • 再次感谢你,安娜。我会立即更新我的代码,稍后我会告诉你我是否能够按照你说的那样工作..
    • 哇,它就像一个魅力......谢谢,谢谢,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多