2012-03-02 10 views
6

私は注釈付きのマップビューを持っており、これらの注釈は吹き出しを表示します。吹き出しの公開の詳細ボタンがクリックされると、新しいビューが表示されます。MKMapViewアノテーションの変更/削除の順序は?

私のMKAnnotationsは<MKAnnotation>を実装するカスタムクラスです。そのクラスMyClassを呼んでみましょう。それらはNSMutableArrayに格納されます。このビューのviewdidload中に、この配列のMyClassの各オブジェクトをマップビューの注釈に追加します。デバッガを使用すると、この追加がすべて完了すると、[self.MapView注釈]の順序はNSMutableArrayと同じになることがわかります。

mapView:viewForAnnotation:内に別のブレークポイントを設定し、1)NSMutableArrayと2)[self.MapView annotations]の順番を確認します。配列はもちろん同じ順序です。ただし、注釈の順序が乱れています。

これは大きな問題でした。なぜなら、ユーザーが次のビューで選択したMyClassの特定のインスタンスを使用する必要があったからです。 AKA、私は注釈を見て、その索引を見つけて、それを使って配列内の同じ索引を取得したいと思っていました。

私は今、注釈を直接保存できることを認識しました(Androidの背景から来て、これは私にとってとても涼しかったです)。しかし、私はまだ注文が混乱した理由について、概念的には紛失している。誰か助けてくれますか?以下のコード:

- (void)viewDidLoad 
{ 


    if([fromString isEqualToString:@"FromList"]) 
     self.navigationItem.hidesBackButton = TRUE; 
    else { 
     self.navigationItem.rightBarButtonItem = nil; 
    } 


    self.array = [MySingleton getArray]; 
    //set up map 

    //declare latitude and longitude of map center 
    CLLocationCoordinate2D center; 
    center.latitude = 45; 
    center.longitude = 45; 

    //declare span of map (height and width in degrees) 
    MKCoordinateSpan span; 
    span.latitudeDelta = .4; 
    span.longitudeDelta = .4; 

    //add center and span to a region, 
    //adjust the region to fit in the mapview 
    //and assign to mapview region 
    MKCoordinateRegion region; 
    region.center = center; 
    region.span = span; 
    MapView.region = [MapView regionThatFits:region]; 

    for(MyClass *t in self.array){ 
     [MapView addAnnotation:t]; 
    } 
    [super viewDidLoad]; 
} 



//this is the required method implementation for MKMapView annotations 
- (MKAnnotationView *) mapView:(MKMapView *)thisMapView 
      viewForAnnotation:(MyClass *)annotation 
{ 


    static NSString *identifier = @"MyIdentifier"; 

    //the result of the call is being cast (MKPinAnnotationView *) to the correct 
    //view class or else the compiler complains 
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[thisMapView 
                    dequeueReusableAnnotationViewWithIdentifier:identifier]; 
    if(annotationView == nil) 
    { 
     annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
    } 

    annotationView.pinColor = MKPinAnnotationColorGreen; 

    //pin drops when it first appears 
    annotationView.animatesDrop=TRUE; 

    //tapping the pin produces a gray box which shows title and subtitle 
    annotationView.canShowCallout = YES; 

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    annotationView.rightCalloutAccessoryView = infoButton; 


    return annotationView; 
} 

答えて

6

あなたがaddAnnotationaddAnnotationsを呼び出し、マップビューは、注釈の内部リストへの参照(複数可)を追加します。

のプロパティは、この内部リスト(タイプに関係なく)をNSArrayとして返します。

annotationsプロパティが注釈を追加したのと同じ順序で配列を返すと書かれている場所はわかりません。showsUserLocationがオンの場合、配列にはその注釈も含められます明示的に追加していませんでしたが。

annotationsプロパティでは、オブジェクトの順序を気にする必要はありません。コードに関する

ちょうど少数の提案:

  • あなたarray<MKAnnotation>を実装するオブジェクトは、代わりにそれをループで、あなたはaddAnnotations(複数)を呼び出すことによって、一発ですべての注釈を追加することができますし、パスが含まれていますので、配列
  • viewForAnnotationには、設定しているプロパティはどれも特定のアノテーションに依存しないため、すべてif (av == nil)ブロック内に設定することができます。このようにして、最大の再利用を実現します。
  • viewForAnnotationの中で、ifの後と外に、ビューのannotationプロパティを現在の注釈に設定する必要があります。これは、ビューが別の注釈から再利用されている場合です。
  • 最後にviewForAnnotationannotationMyClassであると仮定しないでください。 showsUserLocationをオンにすると、そうなることはありません。パラメータをid<MKAnnotation>と宣言し、必要に応じてクラスの内容を確認してからキャストする方が安全です。
+0

は、あなたの最後のポイントをいただき、ありがとうございます。常に学習 –

1

@Annaのように、アノテーションの順序を気にする必要はありません。私の場合はそうではありません。注釈ビューの中には重複するものがあります。重複する2つのビューの上に特定の注釈ビューが常に必要です。したがって、- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotationがアノテーションを追加したのと同じ順序で呼び出されることを期待しているので、アノテーションの順序は意味があります。

EDIT: and the solution is here :-)

+1

リンクされたソリューションは、注釈** _ views _ **の注釈の表示順序に関するものです(OPの質問に関するID ** _ model _ **オブジェクトではありません)。さらに、viewForAnnotationデリゲートメソッドは、追加された後でも同じアノテーションに対して複数回呼び出すことができます(たとえば、マップをスクロール/ズームしてアノテーションが画面に戻った場合など)。あなたの前提は、少数の注釈や特定のOSバージョンで "うまくいく"かもしれませんが、あなたはそれを期待したり頼たりしてはいけません。 – Anna

+0

アンナは、アノテーションのzオーダーは設定できません。 – wreckgar23

関連する問題