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

Objective c ios:MapView Pin颜色注释不断变化;为什么?,objective-c,ios,annotations,mkmapview,Objective C,Ios,Annotations,Mkmapview,你好,谢谢你的帮助 我正在使用以下代码设置MapView注释上的pin颜色 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithI

你好,谢谢你的帮助

我正在使用以下代码设置MapView注释上的pin颜色

- (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];
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
MKPinAnnotationView*pinView=(MKPinAnnotationView*)[mapView出列可重用的AnnotationViewWith Identifier:@“pinView]”;
如果(!pinView){
//////////////
pinView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“pinView”];
pinView.pinColor=MKPinAnnotationColorGreen;
if([[annotation title]isEqualToString:@“MuRoom”])
{
//做点什么
pinView.pinColor=MKPinAnnotationColorRed;
NSLog(@“来自ann索引的数据%@”,annTile);
}
如果([[annotation title]isEqualToString:@“Mike's”])
{
//做点什么
pinView.pinColor=MKPinAnnotationColorRed;
NSLog(@“来自ann索引的数据%@”,annTile);
}
如果([[annotation title]IsequalString:@“Bill's”])
{
//做点什么
pinView.pinColor=MKPinAnnotationColorPurple;
NSLog(@“来自ann索引的数据%@”,annTile);
}
如果([[annotation title]isEqualToString:@“Steve's”])
{
//做点什么
pinView.pinColor=MKPinAnnotationColorGreen;
NSLog(@“来自ann索引的数据%@”,annTile);
}
如果([[annotation title]isEqualToString:@“Louisa's”])
{
//做点什么
pinView.pinColor=MKPinAnnotationColorGreen;
NSLog(@“来自ann索引的数据%@”,annTile);
}
pinView.animatesDrop=是;
pinView.canShowCallout=是;
UIButton*rightButton=[UIButton Button类型:UIButtonTypedTailButton];
pinView.rightCalloutAccessoryView=rightButton;
}否则{
pinView.annotation=注释;
}
返回pinView;
}
然后,我使用下面的功能过滤我的PIN位置
-(无效)筛选器全部:(id)发件人
{
[地图视图删除注释:annoArra];
[地图视图删除说明:AnnoarRay场馆];
[mapview removeAnnotations:artArray];
//CLLocationCoordinate2D center=mapview.centerCoordinate;
//mapview.centerCoordinate=中心;
[地图视图添加注释:annoArra];
[地图视图添加注释:annoArrayVenue];
[mapview addAnnotations:artArray];
}
-(无效)筛选器art:(id)发件人
{
[地图视图删除注释:annoArra];
[mapview removeAnnotations:artArray];
[地图视图删除说明:AnnoarRay场馆];
[地图视图添加注释:annoArra];
}
-(void)FilterVenue:(id)发送方
{
[地图视图删除注释:annoArra];
[mapview removeAnnotations:artArray];
[地图视图删除说明:AnnoarRay场馆];
[mapview addAnnotations:artArray];
}
问题:如何使pin颜色保持其原始颜色?在我过滤后,它们以随机针颜色的形式返回


再次感谢。

出现这种情况是因为您没有正确使用“reuseIdentifier”。当您从dequeueReusableAnnotationViewWithIdentifier:@“pinView”获取pin时,您需要:

始终设置pin颜色,或 对每个彩色pin使用不同的reuseIdentifier

i、 e.您可能会得到一个带有红色图钉的可重用视图,并且希望显示一个蓝色图钉

例如:

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

这是因为您没有正确使用“reuseIdentifier”。当您从dequeueReusableAnnotationViewWithIdentifier:@“pinView”获取pin时,您需要:

始终设置pin颜色,或 对每个彩色pin使用不同的reuseIdentifier

i、 e.您可能会得到一个带有红色图钉的可重用视图,并且希望显示一个蓝色图钉

例如:

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

无知的迷雾正在慢慢消散。再次感谢。无知的迷雾正在慢慢消散。再次感谢。