2012-04-07 16 views
2

MKAnnotationNSArrayがあります。最初の/最後の注釈には赤のピンカラーを設定する必要があります。 プログラムは私が望むように動作しません、緑色のピンは、ランダムな方法で2つの異なる注釈と毎回関連付けられ、最初と最後に関連付けられていません。MKMapViewの最初と最後のMKAnnotationのMKPinAnnotationColorRedを設定します。

ので、これは私が私のコントローラでやっているものです:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //load set a coords from file etc... 
    //self.coordinates is the array of annotations 
    [self.myMap addAnnotations:self.coordinates]; 
} 

その後、viewForAnnotationに:コールバック:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{ 
    NSString *ident = @"MY_IDENTIFIER"; 
    MKPinAnnotationView *annView=(MKPinAnnotationView *)[self.myMap dequeueReusableAnnotationViewWithIdentifier:ident]; 
    if(self.myMap.userLocation==annotation){ 
     return nil; 
    } 
    if(annView == nil){ 
     annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ident]; 
     annView.animatesDrop=TRUE; 
     annView.canShowCallout = YES; 
     annView.rightCalloutAccessoryView=[UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     annView.calloutOffset = CGPointMake(-5, 5); 
     int currIdxAnn=[self.myMap.annotations indexOfObject:annotation]; 
     int lastIdxAnn=[self.myMap.annotations indexOfObject:[self.myMap.annotations lastObject]]; 
     /* 
     if (currIdxAnn==0 || currIdxAnn==lastIdxAnn) { 
      annView.pinColor=MKPinAnnotationColorRed; 
     }else { 
      annView.pinColor=MKPinAnnotationColorGreen; 
     }*/ 
     CustomAnnotation *an=(CustomAnnotation *)annotation; 
     if (an.tag==98||an.tag==99) { 
      annView.pinColor=MKPinAnnotationColorGreen; 
     }else { 
      annView.pinColor=MKPinAnnotationColorRed; 
     } 

    } 
    return annView; 
} 

私が信じているような方法が機能しないaddAnnotations:ようですおそらく、配列とは異なる順序で注釈を読み込むでしょう。出来ますか?
私もdidAddAnnotationViews:で同様のプロセスを試みましたが、良い結果は得られませんでした。

いくつかのヒント?おかげさまで

PS:私がこの質問を書くのを終えて、私はthisという応答を見出し、私の理論を確認しているようです。誰かがすでに同様の問題に遭遇しましたか?

EDIT:いくつかのテストの後、私は欲しいものを達成するための最良の方法は、最初に私の最後の/最初の注釈にタグを設定することであることに気づい:あなたが見ることができるよう

CustomAnnotation *first=[self.coordinates objectAtIndex:0]; 
first.tag=98; 
CustomAnnotation *last=[self.coordinates objectAtIndex:[self.coordinates indexOfObject:[self.coordinates lastObject]]]; 
last.tag=99; 

は、私は少し持っていますアノテーションが私が探していたものであることを理解するためにviewForAnnotationを修正しました。以下のよう その後トリックだけで、バックグラウンドスレッドで注釈を追加する機能を呼び出していました:これはテストの一週間後に私のために働いた

[self performSelectorInBackground:@selector(addAllAnnot) withObject:nil]; 

-(void)addAllAnnot{ 

    [self.myMap addAnnotations:self.coordinates]; 
} 

誰もが任意のより良いアイデアを持っている場合、それは理解されるであろう。

+0

私はあなたが 'MKPinAnnotationView'の代わりに' MKAnnotationView'を使うべきだと思います。 –

+0

@nevankingあなたはより良く説明できますか?私はMKAnnotationViewを使用する必要があります。 – Mat

+0

@nevanking..sorry私は 'MKPinAnnotationView'を使う必要があることを意味しました。 – Mat

答えて

0

マップから返された配列には数えません。おそらくあなたには分かりにくい方法でmapkitによって最適化されているでしょう。

私はあなたが計算した座標から変数を保持したいと思います。または、必要でない場合は、最初と最後の注釈の配列のみを返します。

2

あなたの答えにあなたが参照した答えを見ると、これらの注釈が追加される順序やマップビューの内部リストにあるインデックスを判断する方法がないと思います。

どのようなオプションがありますか?いつものように、MKAnnotationクラスをサブクラス化して、それを使ってどのアノテーションが追加されているかを区別できるプロパティを追加します。したがって、これらのアノテーションを配列に追加するときにプロパティを設定してください...これがあなたの問題を解決することを望みます..

+0

返信いただきありがとうございます、私は、MKAnnotationをサブクラス化しており、属性タグを追加していますので、すべてのタグを配列に格納できます。しかし、奇妙なことに、if文にブレークポイントを挿入すると、プログラムはすべてのステップを正しく実行しますが、関数のスコープを外れた後でなければ、緑のピンはランダムに配置されます。 – Mat

関連する問題