2011-06-23 14 views
6

マップにアノテーションを配置するマップキットアプリケーションがあります。マップキットを押すと、タイトル属性でコールアウトが表示されます。ios mapkitマップをタップしてアノテーションコールアウトを閉じる

これは問題なく動作しますが、ユーザーはそれらを閉じることができません。別の注釈をタップするまで開いています。ユーザーがマップ上でelsehwereをタップ(またはアノテーションをもう一度タップ)して閉じることはできませんか?

私はこれがデフォルト設定だったと感じました。おそらく私がやっていることは、それを詰め込んでいますか?

- (void)handleTap:(UITapGestureRecognizer *)sender {  
if (sender.state == UIGestureRecognizerStateEnded) { 


    CGPoint tapPoint = [sender locationInView:sender.view.superview]; 
    CLLocationCoordinate2D coordinate = [self.mapView convertPoint: tapPoint toCoordinateFromView: self.mapView]; 

    if (pitStopMode && !pitStopMade){ 
      pitStopMade = YES; 
     InfoAnnotation *annotation = [[InfoAnnotation alloc] 
         initNewPitstopWithCoordinate:coordinate]; 
     NSLog(@" Created Pit Stop"); 

     annotation.draggable = NO; 
     //place it on the map 
     [self.mapView addAnnotation: annotation]; 

     self.instructionLabel.text = @"Tap button again to remove"; 
     annotation.creatorId = self.localUser.deviceId; 
     //send it to the server 
     [annotation updateLocationWithServerForConvoy: self.convoyCode];   

     [annotation release]; 

    } 

    if (hazardMode && !hazardMade){ 
      hazardMade = YES; 
     InfoAnnotation *annotation = [[InfoAnnotation alloc] 
         initNewHazardWithCoordinate:coordinate];   
     NSLog(@" Created Hazard"); 

     annotation.draggable = NO; 
     //place it on the map 
     [self.mapView addAnnotation: annotation]; 

     self.instructionLabel.text = @"Tap button again to remove"; 
     annotation.creatorId = self.localUser.deviceId; 
     //send it to the server 
     [annotation updateLocationWithServerForConvoy: self.convoyCode];   

     [annotation release]; 


    } 
} 

}

私はまた、これらのタップが通過行くようにしなければならないものがあります:私は、私はいくつかのマップを検出するために使用するジェスチャー認識がこれを発射

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
              initWithTarget:self action:@selector(handleTap:)]; 
tap.numberOfTapsRequired = 1; 

[self.mapView addGestureRecognizer: tap]; 

をタップする必要がありマップビューに?アノテーションをドラッグしてタップするとうまくいきますが、これが原因なのかどうかはわかりません。

私には欠けているオプションがありますか、これを手動で実装する必要がありますか?

答えて

10

UIGestureRecognizerDelegateメソッドgestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:を実装し、YESを返すことができます(マップビューの独自のタップジェスチャ認識機能もそのメソッドを実行します)。

まず、(コンパイラの警告を回避するために)あなたのビューコントローラのインターフェイスに、プロトコルの宣言を追加します、最後に

tap.delegate = self; 

@interface MyViewController : UIViewController <UIGestureRecognizerDelegate> 

次に、ジェスチャー認識にdelegateプロパティを設定しますメソッドを実装します。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
    shouldRecognizeSimultaneouslyWithGestureRecognizer: 
     (UIGestureRecognizer *)otherGestureRecognizer 
{ 
    return YES; 
} 



それが何らかの理由でうまくいかない場合は、代わりにhandleTap:メソッドの先頭に手動で任意の現在選択されている注釈を選択解除することができますが:

for (id<MKAnnotation> ann in mapView.selectedAnnotations) { 
    [mapView deselectAnnotation:ann animated:NO]; 
} 

マップビューのみの最大を可能にもかかわらず一度に1つの注釈を選択すると、selectedAnnotationsプロパティはNSArrayとなるので、ループします。

0

アンナ説明良い。また、正確なコードで説明したいと思います。あなたはこのようにすることができます

 UITapGestureRecognizer *tapMap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeCallout:)]; 
     [self.mapView addGestureRecognizer:tapMap]; 

-(void) closeCallout:(UIGestureRecognizer*) recognizer 
     { 
      for (id<MKAnnotation> ann in mapView.selectedAnnotations) 
      { 
       [mapView deselectAnnotation:ann animated:NO]; 
      }  


     } 
関連する問題