How to make annotation clikable SwiftUI MapKit

63 Views Asked by At

I'm new to Swift. I managed to integrate a map which allows me to manage the notion of cluster but I cannot make my annotations clickable. In the best of all worlds I would also like to make the clusters clickable and be able to modify their style.

struct MapView: UIViewRepresentable { @EnvironmentObject private var locationManager: LocationManager

var forDisplay = data

class Coordinator: NSObject, MKMapViewDelegate {
    
    var parent: MapView
    
    init(_ parent: MapView) {
        self.parent = parent
    }
    
    /// showing annotation on the map
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard let annotation = annotation as? LandmarkAnnotation else { return nil }
        return CustomAnnotationView(annotation: annotation, reuseIdentifier: CustomAnnotationView.ReuseID)
    }
}



func makeCoordinator() -> Coordinator {
    MapView.Coordinator(self)
}


func makeUIView(context: Context) -> MKMapView {
    ///  creating a map
    let view = MKMapView()
    /// connecting delegate with the map
    view.delegate = context.coordinator
    view.setRegion(locationManager.region, animated: false)
    view.mapType = .standard
    
    for points in forDisplay {
        let annotation = LandmarkAnnotation(coordinate: points.coordinate, color: points.color)
        view.addAnnotation(annotation)
    }
    
    
    return view
    
}

func updateUIView(_ uiView: MKMapView, context: Context) {
    
}

}

struct SampleData: Identifiable { var id = UUID() var latitude: Double var longitude: Double var coordinate: CLLocationCoordinate2D { CLLocationCoordinate2D( latitude: latitude, longitude: longitude) } var color: UIColor }

var data = [ SampleData(latitude: 43.70564024126748, longitude: 142.37968945214223,color: UIColor.purple), SampleData(latitude: 43.81257464206404, longitude: 142.82112322464369,color: UIColor.green), SampleData(latitude: 43.38416585162576, longitude: 141.7252598737476,color: UIColor.yellow), SampleData(latitude: 45.29168643283501, longitude: 141.95286751470724,color: UIColor.orange), SampleData(latitude: 45.49261392585982, longitude: 141.9343973160499,color: UIColor.red), SampleData(latitude: 44.69825427301145, longitude: 141.91227845284203,color: UIColor.blue) ]

class LandmarkAnnotation: NSObject, MKAnnotation { let coordinate: CLLocationCoordinate2D let color: UIColor init(coordinate: CLLocationCoordinate2D, color: UIColor) { self.coordinate = coordinate self.color = color super.init() } }

/// here posible to customize annotation view let clusterID = "clustering"

class CustomAnnotationView: MKMarkerAnnotationView {

static let ReuseID = "customAnnotation"

let customSwiftUIView = LocationMapAnnotationView()

override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
    super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)

    clusteringIdentifier = "customCluster"

    // Désactivez la superposition par défaut de la bulle d'annotation
    canShowCallout = false
    
    // Convertissez la vue SwiftUI en UIView
    let uiViewRepresentable = UIHostingController(rootView: customSwiftUIView)
    if let uiView = uiViewRepresentable.view {
        markerTintColor = UIColor.clear
        glyphText = ""
        
        addSubview(uiView)
            
    }

}


required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

[enter image description here](https://i.stack.imgur.com/TXK58.png)

0

There are 0 best solutions below