0

I am trying to give my two MKPointAnnotation pins a custom image. So far I am being able to display the same image on both pins. What could I do to the code below to give each pin a custom image?

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

        guard !(annotation is MKUserLocation) else {
            return nil
        }


        let annotationIdentifier = "AnnotationIdentifier"

        var annotationView: MKAnnotationView?

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


        if let annotationView = annotationView {
            annotationView.canShowCallout = true
            annotationView.image = UIImage(named: "Moto_pin")
        }

        return annotationView
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
cosmos
  • 153
  • 1
  • 15

2 Answers2

0

Instead of annotationView.image = UIImage(named: "Moto_pin"), you will want to change the image for each pin. That line will assign the same image to each annotation view.

AdamPro13
  • 7,232
  • 2
  • 30
  • 28
0

Try to use MKPinAnnotationView instead of MKAnnotationView. To do so you just need to change:

var annotationView: MKPinAnnotationView?

With this class you can customize your pin color and the whole view

kgkoutio
  • 89
  • 4