Skip to content

Cannot click on annotations  #139

@kevinma0522

Description

@kevinma0522

Description
Hi. I have implemented a custom view for un-clustered annotations, and I'm noticing that it is impossible to tap them, aka, buttons are unclickable in the annotation. I am working in SwiftUI, making my view a MKMapViewRepresentable to wrap the UIKit components, and calling the different views like so:

class Coordinator: NSObject, MKMapViewDelegate, ClusterManagerDelegate {
    var parent: MKMapViewRepresentable
    var clusterManager: ClusterManager
    @ObservedObject var mapViewModel: MapViewModel = MapViewModel.shared
    var region: MKCoordinateRegion
    init(_ parent: MKMapViewRepresentable, clusterManager: ClusterManager, region: MKCoordinateRegion) {
        self.parent = parent
        self.clusterManager = clusterManager
        self.region = region
    }
    func cellSize(for zoomLevel: Double) -> Double? {
            return nil
        }
        
    func shouldClusterAnnotation(_ annotation: MKAnnotation) -> Bool {
        return true
    }
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        print("annotation", annotation)
        let identifier = "CustomAnnotationView"
        clusterManager.reload(mapView:mapView)
        if let cluster = annotation as? ClusterAnnotation {
                        return CountClusterAnnotationView(annotation: cluster, reuseIdentifier: "CountClusterView")
                    }
        else {
            print("non clustered")
            let customView = CustomAnnotationView(region: region, annotation: annotation, reuseIdentifier: "CustomAnnotationView")
            return customView
        }
    }

With my CustomAnnotationView looking like this to call AnnotationView (which is my true custom display)

override var annotation: MKAnnotation? {
        willSet {
            hostingController?.view.removeFromSuperview()
            hostingController = nil

            if let customAnnotation = newValue as? CustomAnnotation {
                let annotationView = AnnotationView(region: region, annotation: customAnnotation)
                let hostingController = UIHostingController(rootView: annotationView)
                hostingController.view.frame = self.bounds
                hostingController.view.backgroundColor = UIColor.clear
                hostingController.view.isUserInteractionEnabled = true

                self.addSubview(hostingController.view)
                self.hostingController = hostingController
            }
        }

In the AnnotationView, I have a button in the ZStack, whose action never gets called when tapped. I have tried all sorts of work arounds like making other things in the Stack ontapGesture but annotations just don't seem to be tappable when the library is wrapped in all these UIKit things when used in SwiftUI.

Checklist

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions