2011-08-16 17 views
2

私のアプリには、注釈の3つの配列、フードノーテーション、ガス注釈、ショッピングノートがあります。注釈の各配列に異なる色のピンを表示する必要があります。私は現在使用中です注釈の複数の配列、各配列の異なるピンカラー?

- (MKAnnotationView *)mapView:(MKMapView *)sheratonmap viewForAnnotation:(id<MKAnnotation>)annotation { 
    NSLog(@"Welcome to the Map View Annotation"); 

    if([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    static NSString* AnnotationIdentifier = @"Annotation Identifier"; 
    MKPinAnnotationView* pinview = [[[MKPinAnnotationView alloc] 
            initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease]; 

    pinview.animatesDrop=YES; 
    pinview.canShowCallout=YES; 
    pinview.pinColor=MKPinAnnotationColorPurple; 

    UIButton* rightbutton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [rightbutton setTitle:annotation.title forState:UIControlStateNormal]; 
    [rightbutton addTarget:self action:@selector(showDetails) forControlEvents:UIControlEventTouchUpInside]; 
    pinview.rightCalloutAccessoryView = rightbutton; 

    return pinview; 
} 

アノテーションの配列ごとに3種類のピンカラーを使用するように設定するにはどうすればよいですか。

ありがとうございます!

答えて

2

MKAnnotationというカスタムを作成し、さまざまな種類のアノテーションを区別するためのカスタムプロパティ(おそらくtypedef列挙型)を持つことをお勧めします。その後

typedef enum { 
    Food, 
    Gas, 
    Shopping 
} AnnotationType 

あなたは条件付きでif (annotation.annotationType == Food) { set pinColor }あなたは明確コードを有するためAnnotationTypeためにswitch文を使用することができます。もちろん、

あなたの色を設定することができます

switch(annotation.annotationType) { 
    case Food: 
     do something; 
     break; 
    case Gas: 
     do something; 
     break; 
    case Shopping: 
     do something; 
     break; 
} 

詳細は、次の質問を参照してください。色を追加する(後でアプリを拡張したい場合):

MKPinAnnotationView: Are there more than three colors available?


ここの重い修正を示しtutorialからコードスニペットです:

calloutMapAnnotationView.contentHeight = 78.0f; 
UIImage *asynchronyLogo = [UIImage imageNamed:@"asynchrony-logo-small.png"]; 
UIImageView *asynchronyLogoView = [[[UIImageView alloc] initWithImage:asynchronyLogo] autorelease]; 
asynchronyLogoView.frame = CGRectMake(5, 2, asynchronyLogoView.frame.size.width, asynchronyLogoView.frame.size.height); 
[calloutMapAnnotationView.contentView addSubview:asynchronyLogoView]; 

HTH

+0

私はこれをmkpinannotationviewとpinview.pincolor = ____の組み込み関数を使って行うことはできません。 – Matt

+0

はい、あなたは 'MKAnnotationView'のpinColorプロパティを使ってそれを行うこともできます。また、より関連性の高いリンクで質問を更新しました。 –

+0

ありがとうございました!私は今すぐその仕事をするつもりです! – Matt

6

はあなたがその識別いくつかのプロパティを追加したMKAnnotationプロトコルを実装するカスタムクラスを定義していますそれはどのような注釈ですか?または、MKAnnotationを実装する3つの別々のクラス(アノテーションの種類ごとに1つ)を定義しましたか?あなたが呼ばれるint性質を持っていたもので、アノテーションクラスを定義したと仮定すると、

は、あなたがviewForAnnotationでこれを行うことができ、あなたのアノテーションクラスにannotationTypeを言う:

int annType = ((YourAnnotationClass *)annotation).annotationType; 
switch (annType) 
{ 
    case 0 : //Food 
     pinview.pinColor = MKPinAnnotationColorRed; 
    case 1 : //Gas 
     pinview.pinColor = MKPinAnnotationColorGreen; 
    default : //default or Shopping 
     pinview.pinColor = MKPinAnnotationColorPurple; 
} 


他の物事のカップル:

  • あなたはaddTaを使用しての
  • 代わりdequeueReusableAnnotationViewWithIdentifier:メソッドを使用する必要がありますrget:アクションと吹き出しボタンの押下を処理するカスタムメソッド、マップビューのデリゲートメソッドcalloutAccessoryControlTappedを使用します。そのデリゲートメソッドでは、view.annotationを使用してアノテーションにアクセスできます。