2

こんにちは、助けてくれてありがとう。

次のコードを使用して、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];
}

質問:ピンの色を元の色に保つにはどうすればよいですか?フィルタリングした後、それらはランダムなピンカラーとして戻ってきます。

再度、感謝します。

4

1 に答える 1

4

これは、「reuseIdentifier」を適切に使用していないために発生します。dequeueReusableAnnotationViewWithIdentifier:@ "pinView"からピンを取得する場合は、次のいずれかを行う必要があります。

常にピンの色を設定するか、色付きのピンごとに異なる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);
        }
于 2012-12-31T21:08:19.287 に答える