0

複数の注釈が付いた地図があります。最初と最後の注釈を表示することができました。各注釈に異なる色を付けたいです。

これが私の注釈を挿入する方法の私のコードです

if(i<1 || i >object.count-2)            
{    
        MyAnnotation* myAnnotation1=[[MyAnnotation alloc] init];                        
        myAnnotation1.coordinate=theCoordinate1;
        myAnnotation1.title=DEVNAME;
        myAnnotation1.subtitle=it.address;                        
        [mapView addAnnotation:myAnnotation1];                        
        [annotations addObject:myAnnotation1];                             
}

if条件は、配列のインデックスを読み取って、最初と最後の注釈のみを削除することです。

これが、ピンをマップにドロップする方法です...

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
    (id <MKAnnotation>)annotation {
    MKPinAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation)  
    {

        static NSString* MyAnnotationIdentifier = @"MyAnnotationIdentifier";
        MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
                                               initWithAnnotation:annotation reuseIdentifier:MyAnnotationIdentifier] autorelease];
        customPinView.pinColor = MKPinAnnotationColorRed;
        customPinView.animatesDrop = YES;
        customPinView.canShowCallout = YES;



       UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        customPinView.rightCalloutAccessoryView = rightButton;

        return customPinView;
    }
    else
    {
        pinView.annotation = annotation;
    }
    return pinView;
  }

注釈の色を変える方法は?

4

2 に答える 2

0

MKAnnotationView クラスを使用して、各ピンのカスタム イメージを設定します。

MKAnnotationView *customPinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
customPinView.image = [UIImage imageNamed:@"pin1.png"];
.
.
.

アプリ バンドルの画像を使用する代わりに、独自の色を実現できるようにプログラムで UIImage を作成する必要がある場合があります。これはあなた次第です。

さらに、注釈に値を設定して、どの注釈がどの画像を取得するかを識別できます。MKAnnotation をサブクラス化して、pinNumber という整数プロパティを追加します。

myAnnotation.pinNumber = 2;

customPinView.image = [UIImage imageNamed:[NSString stringWithFormat:@"pin%d.png",annotation.pinNumber]];
于 2012-09-11T12:35:43.640 に答える
0

もちろん、最初と最後のピンを除いては機能しません。残りのピンでelseは、コードの一部が実行されます。

だからそこにあるMKPinAnnotationView *pinViewでしょうnil。したがって、メモリが割り当てられていない注釈は、色を変更できません!!! :-)

必要allocに応じてinitMKPinAnnotationView *pinViewfor else の部分をどこかに配置する必要があります。

于 2012-09-11T12:36:41.330 に答える