didSelect for MKAnnotationView not firing

I have troubles to make a custom annotation at a MapView selectable. I was trying several ways, without success:

  • Using didSelect of MKMapViewDelegate
  • Using a UIButton which is added to the AnnotationView
  • Using UITapGestureRecognizer for the ImageView, which should be used for the annotation

You can find all these approaches commented in the code below:

override func viewDidLoad() {
    super.viewDidLoad()
    // some more initialization code

    for item in items {
        let annotation = IdentifiedMKPointAnnotation() // has only one additional property named "id"

        annotation.coordinate = CLLocationCoordinate2D(latitude: item.spots[0].latitude!, longitude: item.spots[0].longitude!)
        annotation.id = i

        self.mapView.addAnnotation(annotation)

        i += 1
    }
}


// MARK: - MapViewDelegate
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let item = self.items[(annotation as! IdentifiedMKPointAnnotation).id!]

    let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "")
    //annotationView.canShowCallout = false
    //annotationView.isEnabled = false

    let imageView = UIImageView()
    imageView.frame.size = CGSize(width: 40, height: 40)
    imageView.layer.cornerRadius = 20
    imageView.layer.masksToBounds = true
    imageView.af_setImage(withURL: URL(string: item.images.count > 0 ? item.images[0] : item.image!)!)
    imageView.contentMode = .scaleAspectFill

    //let gr = UIGestureRecognizer(target: self, action: #selector(annotationSelected(_:)))
    //imageView.addGestureRecognizer(gr)
    //imageView.isUserInteractionEnabled = true

    //let button = UIButton()
    //button.frame.size = CGSize(width: 40, height: 40)
    //button.setImage(#imageLiteral(resourceName: "play_arrow"), for: .normal)
    //button.addTarget(self, action: #selector(annotationSelected(_:)), for: .touchUpInside)

    annotationView.addSubview(imageView)
    //annotationView.addSubview(button)

    return annotationView
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    performSegue(withIdentifier: "showDetail", sender: self.items[(view.annotation as! IdentifiedMKPointAnnotation).id!])

    view.setSelected(false, animated: false)
}

func annotationSelected(_ sender: UIImageView) {
    performSegue(withIdentifier: "showDetail", sender: self.items[((sender.superview as! MKAnnotationView).annotation as! IdentifiedMKPointAnnotation).id!])
}

Any ideas why the action(s) isn't/aren't firing?

If the annotation's title is blank, didSelect delegate is not called. Try the following code.

let annotation = IdentifiedMKPointAnnotation()
annotation.title = "title"

MapView didSelectAnnotationView is not called � Issue #35, GitHub is home to over 50 million developers working together to host didSelectAnnotationView:(MKAnnotationView *)view { view.selected� Not only my MapPins, but also the blue location circle counts as mapView annotation. Therefore, if the location service is enabled my updateUIView() removes and adds all my annotations on every didSelect because self.displayedPins.count != mapView.annotations.count is always false which causes the sudden disappearance of the bottom sheet.

Just found the solution based on the following post: https://stackoverflow.com/a/28671680/6010489

It's enough to set height and width of the annotationview:

annotationView.frame.size.height = 40
annotationView.frame.size.width = 40

didSelectAnnotationView, didSelectAnnotationView – Handle User Tap Event on MKAnnotationView other wise this function will be called every time when user location changes. Your browser does not currently recognize any of the video formats available. I am using Xmarin to create a map with custom annotations and for some reason the DidSelectAnnotationView method is not called once i tap on annotations. Here is how I am creating the annotations:

For some reason I had the same problem when mapView's delegate was set in Storyboard (Xcode 10.2, Swift 5.0). So this was not working:

However, when I removed the connection and did it in code instead it worked fine:

@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()

    mapView.delegate = self
}

A Swift 5 bug maybe? Storyboard solution works with Objective-C.

mapView(_:didSelect:), mapView(_:didSelect:) Tells the delegate that one of its func mapView( MKMapView, didDeselect: MKAnnotationView). Tells the delegate that one of its � thanks for your help! but this is not the solution. the code is now : ` didSelect view: MKAnnotationView) { let smallSquare = CGSize(width: 30, height: 30) // The size of the button. let button = UIButton(frame: CGRect(origin: CGPoint.zero, size: smallSquare)) button.addTarget(self, action: #selector(ViewController.test), for: .touchUpInside) view.leftCalloutAccessoryView = button print

How to add a button to an MKMapView annotation, The built-in MKPinAnnotationView annotation view has a attached to it, because there's a separate method that gets called when it's tapped. To answer the original question, didSelectAnnotationView was most likely not getting called because the map view's delegate property was not set. Another common cause is if the annotation's title is blank.

Customize MKAnnotation Callout View?, didSelect for MKAnnotationView not firing, addSubview(button) return annotationView } func mapView(_ mapView: MKMapView, didSelect view:� I have added a tittle to the annotation but, even - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view still would not fire. – ben Jun 26 '14 at 10:47 its getting called for me.check for MKMapViewDelegate is set properly or not.

How To Completely Customise Your Map Annotations Callout Views , Alright, time to implement the custom MKAnnotation class that will handle a reference for all the The method above will be called each time an annotation is about to show in the map. for the annotation and hence you tell the system NOT to show the default provided callout. and in the didSelect func My custom MKAnnotationView subclass do not set it's image property. Instead, it handle some subviews to be displayed. I implement public func mapView(_ mapView: MKMapView, didSelect view:

Comments
  • I don't see setting the delegate anywhere in your code. Did you actually do it?
  • yes i did it already in the storyboard. the strange thing is, that "didSelect" works, when i tap a little bit outside of the rounded imageview