Ios mapkit closes annotation leaders by clicking on the map

I have a mapkit application that puts annotations on a map, when you click on them, it shows a leader with a title attribute.

This works fine, but the user cannot close them. They remain open until they touch another annotation. Can't I make it so that the user can click elsehwere on the map (or touch the annotation again) to close it?

I had the feeling that it was the default setting, so maybe something I do is fill it? I have a gesture recognizer that I use to detect some card taps.

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

which causes this:

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

}

Is there anything I need to do to allow these taps to go to the map? Dragging and listening to annotations works great, although I'm not sure if this causes it?

Is there an option that I am missing, or do I need to try to implement it manually?

+6
source share
2 answers

You can implement the UIGestureRecognizerDelegate gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: method and return YES (so its own type of gesture recognition in the form of a map will be executed by its method).

First, add a protocol declaration to your view controller interface (to avoid a compiler warning):

 @interface MyViewController : UIViewController <UIGestureRecognizerDelegate> 

Then set the delegate property in the gesture recognizer:

 tap.delegate = self; 

Finally, we implement the method:

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



If for some reason this does not work, you can alternatively cancel any currently selected annotation manually at the top of the handleTap: method:

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

Although the map view allows you to select at most one annotation at a time, the selectedAnnotations property is an NSArray , so we scroll through it.

+10
source

Anna explains well. I also want to explain it with code. You can do it

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

Source: https://habr.com/ru/post/891183/


All Articles