ios:MapView Pin颜色注释不断变化;为什么?

时间:2012-12-31 21:03:06

标签: objective-c ios annotations mkmapview

您好,感谢您的帮助。

我使用以下代码在MapView注释上设置引脚颜色?

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




    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"];
    if (!pinView) {

        //////////////
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"];
        pinView.pinColor = MKPinAnnotationColorGreen;
        if([[annotation title] isEqualToString:@"MuRoom"])
        {
            // Do somethingMKAnnotation
            pinView.pinColor = MKPinAnnotationColorRed;

            NSLog( @"data from ann index %@", annTile);
        }
        if([[annotation title] isEqualToString:@"Mike's"])
        {
            // Do somethingMKAnnotation
            pinView.pinColor = MKPinAnnotationColorRed;

            NSLog( @"data from ann index %@", annTile);
        }
        if([[annotation title] isEqualToString:@"Bill's"])
        {
            // Do somethingMKAnnotation
            pinView.pinColor = MKPinAnnotationColorPurple;

            NSLog( @"data from ann index %@", annTile);
        }
        if([[annotation title] isEqualToString:@"Steve's"])
        {
            // Do somethingMKAnnotation
            pinView.pinColor = MKPinAnnotationColorGreen;

            NSLog( @"data from ann index %@", annTile);
        }
        if([[annotation title] isEqualToString:@"Louisa's"])
        {
            // Do somethingMKAnnotation
            pinView.pinColor = MKPinAnnotationColorGreen;

            NSLog( @"data from ann index %@", annTile);
        }


        pinView.animatesDrop = YES;
        pinView.canShowCallout = YES;

        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinView.rightCalloutAccessoryView = rightButton;
    } else {
        pinView.annotation = annotation;
    }
    return pinView;
}


I am then using the function below to filter my pins locaions



-(void)FilterAddAll:(id)sender
{
    [mapview removeAnnotations:annoArra];
    [mapview removeAnnotations:annoArrayVenue];
 [mapview removeAnnotations:artArray];


//    CLLocationCoordinate2D center = mapview.centerCoordinate;
//    mapview.centerCoordinate = center;


    [mapview addAnnotations:annoArra];
    [mapview addAnnotations:annoArrayVenue];
    [mapview addAnnotations:artArray];

}

-(void)FilterArt:(id)sender
{
    [mapview removeAnnotations:annoArra];
    [mapview removeAnnotations:artArray];
    [mapview removeAnnotations:annoArrayVenue];

    [mapview addAnnotations:annoArra];
}

-(void)FilterVenue:(id)sender
{
    [mapview removeAnnotations:annoArra];
    [mapview removeAnnotations:artArray];
    [mapview removeAnnotations:annoArrayVenue];

    [mapview addAnnotations:artArray];
}

问题:如何获得针脚颜色以保留其原始颜色?在我过滤后,它们会以随机引脚颜色返回。

再次感谢。

1 个答案:

答案 0 :(得分:4)

这是因为您没有使用&#34; reuseIdentifier&#34;正常。当您从dequeueReusableAnnotationViewWithIdentifier获取一个引脚时:@&#34; pinView&#34;,您需要:

始终设置引脚颜色,或者 为每个彩色引脚使用不同的reuseIdentifier

即。您可能正在使用红色引脚获得可重复使用的视图,并且您希望显示蓝色引脚

示例:

MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"];
    if (!pinView) {

        //////////////
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"];

        pinView.animatesDrop = YES;
        pinView.canShowCallout = YES;

        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinView.rightCalloutAccessoryView = rightButton;
    } else {
        pinView.annotation = annotation;
    }

// SET THE PIN COLOR REGARDLESS OF WHETHER A REUSABLE ANNOTATION WAS RETURNED OR NOT

        pinView.pinColor = MKPinAnnotationColorGreen;
        if([[annotation title] isEqualToString:@"MuRoom"])
        {
            // Do somethingMKAnnotation
            pinView.pinColor = MKPinAnnotationColorRed;

            NSLog( @"data from ann index %@", annTile);
        }
        if([[annotation title] isEqualToString:@"Mike's"])
        {
            // Do somethingMKAnnotation
            pinView.pinColor = MKPinAnnotationColorRed;

            NSLog( @"data from ann index %@", annTile);
        }
        if([[annotation title] isEqualToString:@"Bill's"])
        {
            // Do somethingMKAnnotation
            pinView.pinColor = MKPinAnnotationColorPurple;

            NSLog( @"data from ann index %@", annTile);
        }
        if([[annotation title] isEqualToString:@"Steve's"])
        {
            // Do somethingMKAnnotation
            pinView.pinColor = MKPinAnnotationColorGreen;

            NSLog( @"data from ann index %@", annTile);
        }
        if([[annotation title] isEqualToString:@"Louisa's"])
        {
            // Do somethingMKAnnotation
            pinView.pinColor = MKPinAnnotationColorGreen;

            NSLog( @"data from ann index %@", annTile);
        }