Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot click on annotations #139

Open
kevinma0522 opened this issue Dec 28, 2023 · 0 comments
Open

Cannot click on annotations #139

kevinma0522 opened this issue Dec 28, 2023 · 0 comments

Comments

@kevinma0522
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant