2

iOS6では、画像、タイトル、説明などの詳細を含む注釈のカスタムコールアウトを作成しようとしています。ここで関連する投稿を見ましたが、探しているものではありません。私のアプリでは、注釈を選択するときに、4つのボタンを選択できます。今、私は詳細なコールアウトである4番目のボタンのオプションを作成しようとしています。

カスタムコールアウトを表すUIViewクラスを作成しました。

編集:

Live2Enjoy7の助けを借りて、コードを次のように変更しました。

BuildingViewController.m

- (void)MapMenu:(MapMenu *)menu didSelectButton:(NSInteger)index{

    if (index == 3) {

        image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
        _buildingShowCallout = YES;
        [parentView removeAnnotation:self];
        [parentView addAnnotation:self];
    }
}

ユーザーがボタンを押した場合、このメソッドは、(URLリンクを使用して)新しい画像を作成し、_buildingShowCallout = YESを設定し、最後に選択したアノテーションを削除して再度追加し、viewForAnnotationメソッドを機能させます。

私のMapViewController.mで

- (MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id<MKAnnotation>)annotation{

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

    static NSString *identifier = @"MyAnnotation";
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    BuildingViewController* building = (BuildingViewController*) annotation;

    // If a new annotation is created
    if (annotationView == nil) {

        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:building reuseIdentifier:identifier];

        if (building.buildingShowCallout) {// Create the custom callout
            BuildingCallOut* buildingCallout = [[BuildingCallOut alloc]initWithFrame:CGRectMake(0, 0, 200, 150)];
            [buildingCallout showCallout:building];
            [annotationView addSubview:buildingCallout];//add the new callout as subview of the selected annotation.
        }

    } else{

        annotationView.annotation = annotation;

        if (building.buildingShowCallout) {// Create the custom callout
            BuildingCallOut* buildingCallout = [[BuildingCallOut alloc]initWithFrame:CGRectMake(0, 0, 200, 150)];
            [buildingCallout showCallout:building];
            [annotationView addSubview:buildingCallout];//add the new callout as subview of the selected annotation.
        }
    }

    return annotationView;
}

それはうまく機能していますが、今私は1つの新しい問題を抱えています。

  1. ある注釈のコールアウトを表示することを選択してから、別の注釈のコールアウトを表示することを選択した場合、前の注釈はマップ上に残ります。どうすればこれを解決できますか?

スクリーンショット![ここに画像の説明を入力してください] [1]

ご覧のとおり、画面の上部にある以前のコールアウトは、下部にある新しいコールアウトを呼び出しているときに残ります。「消える」線をどこに置くかを見つけるのに苦労しています。前もって感謝します

4

1 に答える 1

1

あなたの BuildingViewController はあなたの MapViewController だと思います。

ヘッダーで MKMapViewDelegate として宣言します。これにより、表示される注釈やピンのビューを変更できるいくつかのメソッドにアクセスできるようになります。

まず、MapKit/MapKit.h と UIKit/UIKit.h をまだインポートしていない場合はインポートします。

次に、BuildingViewController が MKMapViewDelegate プロトコルに準拠していることを宣言します (上記のとおり)。

3 番目に、次のように ViewDidLoad で BuildingViewController をマップ ビューのデリゲートとして宣言します。

self.mapView.delegate=self; //assuming that the you have a MKMapView named MapView in your layout

4 番目に、次のプロトコル メソッドを実装します。

-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation;

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view;

最初の方法では、左右のコール アウト アクセサリを設定したり、他の多くのことを変更したりできます (ここで CGRectMakes を実行します)。

2 番目の方法では、注釈ビューに実際に表示されるデータを決定します。

MKMapViewDelegate のドキュメントを検索 -->ここ

于 2012-11-24T22:39:10.010 に答える