Individual custom annotation callout swift

212 views Asked by At

I am creating a map which contains both custom annotation pins as well as images in the annotation callouts. The only problem is that I cannot figure out a way to have different images for different annotations. More specifically, I'm after having individual images for the annotation callouts. While I have found results on how to change the pin, I am struggling with finding any info for the callout and would appreciate any help. Below is the function for the annotations I am working with. Thank you.

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    guard !annotation.isKind(of: MKUserLocation.self) else {
        return nil}

    let annotationIdentifier = "AnnotationIdentifier"

    var annotationView: MKAnnotationView?

    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
        annotationView = dequeuedAnnotationView
        annotationView?.annotation = annotation
    } else {
        let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        av.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
        annotationView = av
    }

    if let annotationView = annotationView {
        // Configure your annotation view here
        annotationView.canShowCallout = true
        annotationView.image = UIImage(named: "pinAnnotation.png")
    }

    let imageName = "until.png"
    let image = UIImage(named: imageName)
    let imageView = UIImageView(image: image!)
    annotationView?.detailCalloutAccessoryView = imageView
    return annotationView
}


class ViewController2: UIViewController {

@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()

//Location for first Pin
let locationOne = CLLocationCoordinate2DMake(-47.016945, 167.852095)

//Location for map centering
let locationNZ = CLLocationCoordinate2DMake(-43.937462, 170.507813)
let span = MKCoordinateSpanMake(9, 9)
let region = MKCoordinateRegion(center: locationNZ, span: span)
mapView.setRegion(region, animated: true)

//Create annotation one
let annotation = MKPointAnnotation()
annotation.coordinate = locationOne
annotation.subtitle = "Park"
annotation.title = "Rakiura National Park"

//Add annotation to the map
mapView.addAnnotation(annotation)

mapView.delegate = self}
0

There are 0 answers