MKAnnotation
のNSArray
があります。最初の/最後の注釈には赤のピンカラーを設定する必要があります。 プログラムは私が望むように動作しません、緑色のピンは、ランダムな方法で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];
}
誰もが任意のより良いアイデアを持っている場合、それは理解されるであろう。
私はあなたが 'MKPinAnnotationView'の代わりに' MKAnnotationView'を使うべきだと思います。 –
@nevankingあなたはより良く説明できますか?私はMKAnnotationViewを使用する必要があります。 – Mat
@nevanking..sorry私は 'MKPinAnnotationView'を使う必要があることを意味しました。 – Mat