2011-06-23 89 views
6

Tôi đã có ứng dụng bản đồ để đặt chú thích trên bản đồ, khi bạn nhấn vào chú thích, nó sẽ hiển thị chú dẫn có thuộc tính tiêu đề.ios mapkit đóng chú thích chú thích bằng cách chạm vào bản đồ

Tính năng này hoạt động tốt nhưng người dùng không thể đóng chúng. Chúng vẫn mở cho đến khi chúng nhấn vào một chú thích khác. Tôi không thể làm cho nó mà người dùng có thể khai thác khác trên bản đồ (hoặc bấm vào chú thích một lần nữa) để đóng nó?

Tôi có cảm giác đây là cài đặt mặc định, vì vậy có lẽ điều tôi đang làm đang nhồi nhét nó? Tôi có một nhận dạng cử chỉ mà tôi sử dụng để phát hiện một số đồ vòi

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

[self.mapView addGestureRecognizer: tap]; 

bắn những viên đạn này:

- (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]; 


    } 
} 

}

Có điều gì tôi phải làm cũng để cho những vòi nước đi qua vào mapview? Kéo và khai thác trên chú thích hoạt động tốt mặc dù vậy tôi không chắc chắn nếu đây là những gì gây ra nó?

có tùy chọn nào tôi bị thiếu hoặc tôi có phải thử và triển khai thủ công điều này không?

Trả lời

10

Bạn có thể triển khai phương pháp UIGestureRecognizerDelegategestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: và trả lại YES (do đó trình nhận dạng cử chỉ của chế độ xem bản đồ của chế độ xem bản đồ cũng sẽ thực thi phương thức của nó).

Đầu tiên, hãy thêm miêu tả giao thức để giao diện điều khiển quan điểm của bạn (để tránh một cảnh báo trình biên dịch):

@interface MyViewController : UIViewController <UIGestureRecognizerDelegate> 

Tiếp theo, thiết lập các delegate tài sản trên các trình nhận dạng cử chỉ:

tap.delegate = self; 

Cuối cùng, triển khai phương thức:

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



Nếu điều đó không làm việc ra đối với một số lý do, bạn có thể luân phiên de-chọn bất kỳ chú thích hiện đang được chọn bằng tay ở phía trên cùng của handleTap: phương pháp:

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

Mặc dù xem bản đồ chỉ cho phép tối đa là một chú thích được chọn tại một thời điểm, thuộc tính selectedAnnotations là một số NSArray để chúng tôi lặp lại nó.

0

Anna giải thích tốt.Ngoài ra tôi muốn giải thích bằng mã chính xác. Bạn có thể làm như thế này

 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]; 
      }  


     } 
Các vấn đề liên quan