Hello everybody,
I am trying to create a custom annotation for a map view. Everything works perfectly, except for the UIImage I am trying to show that does not appear.
I think I am not being able to show the MKAnnotationView I am trying to set.
In the mapView method I am not being able to perform that print statement. I can't figure out why.
Thank you!
I am trying to create a custom annotation for a map view. Everything works perfectly, except for the UIImage I am trying to show that does not appear.
I think I am not being able to show the MKAnnotationView I am trying to set.
In the mapView method I am not being able to perform that print statement. I can't figure out why.
Code Block import SwiftUI import UIKit import MapKit class ObservationAnnotation: NSObject, MKAnnotation { let coordinate: CLLocationCoordinate2D let title: String? let subtitle: String? var image: UIImage? init(coordinate: CLLocationCoordinate2D, species: String, image: UIImage, confidence: Double) { self.coordinate = coordinate self.title = species self.subtitle = "Confidence: \(confidence)" self.image = image } } class ObservationAnnotationView: MKAnnotationView { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override init(annotation: MKAnnotation?, reuseIdentifier: String?) { super.init(annotation: annotation, reuseIdentifier: reuseIdentifier) guard let observationAnnotation = self.annotation as? ObservationAnnotation else { return } image = observationAnnotation.image } } struct MyObservationsMap: UIViewRepresentable { var observationList: [Observation] func makeUIView(context: Context) -> MKMapView { MKMapView.init(frame: .zero) } func updateUIView(_ uiView: MKMapView, context: Context) { for observation in observationList { let annotation = ObservationAnnotation( coordinate: CLLocationCoordinate2D(latitude: observation.latitude, longitude: observation.longitude), species: observation.speciesName!, image: UIImage(data: observation.image!)!, confidence: observation.confidence ) uiView.addAnnotation(annotation) } } func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { let reuseId = "observation" let annotationView = ObservationAnnotationView(annotation: annotation, reuseIdentifier: reuseId) annotationView.canShowCallout = true print("Here") return annotationView } }
Thank you!