2011-06-23 10 views
6

Tengo una aplicación de mapas que coloca anotaciones en el mapa, cuando las presiona, muestra la leyenda con el atributo de título.ios mapkit cierre las anotaciones de las anotaciones tocando el mapa

Esto funciona bien, pero el usuario no puede cerrarlos. Permanecen abiertos hasta que toquen otra anotación. ¿No puedo hacer que el usuario pueda tocar elsehwere en el mapa (o tocar la anotación nuevamente) para cerrarlo?

Tenía la sensación de que esta era la configuración predeterminada, ¿entonces quizás algo que estoy haciendo es rellenarlo? Tengo un reconocedor gesto que yo uso para detectar alguna mapa grifos

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

[self.mapView addGestureRecognizer: tap]; 

que dispara la siguiente:

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


    } 
} 

}

¿Hay algo que tengo que hacer para que también estos grifos pasan por a la vista del mapa? Arrastrar y tocar en las anotaciones funciona bien, ¿así que no estoy seguro de si esto es lo que está causando?

¿Existe una opción que me falta, o tengo que intentar implementarla manualmente?

Respuesta

10

Puede implementar el método UIGestureRecognizerDelegategestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: y devolver YES (por lo que el reconocedor de gestos de toque de la vista del mapa también ejecutará su método).

primer lugar, añade la declaración protocolo para la interfaz del controlador de vista (para evitar una advertencia del compilador):

@interface MyViewController : UIViewController <UIGestureRecognizerDelegate> 

A continuación, establezca la propiedad delegate en el reconocedor gesto:

tap.delegate = self; 

Por último, implementar el método:

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



Si eso no funciona por alguna razón, puede, alternativamente, de-seleccione cualquier anotación seleccionada manualmente en la parte superior de la handleTap: método:

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

A pesar de que la vista del mapa sólo permite un máximo de una anotación que se seleccionará a la vez, la propiedad selectedAnnotations es NSArray, así que la recorremos.

0

Anna explique bien.También quiero explicar exactamente el código. Puede hacer esto

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


     } 
Cuestiones relacionadas