Search code examples
iosswiftannotationsmapkitmkannotation

How to set DIFFERENT image pins in different locations SWIFT


I'm trying to code an app that displays different images on a AppleMap. I managed to set one image pin but I don't know how to set different images.

In my view didLoad I wrote :

self.pin = AnnotationPin(title: sport, subtitle: "ouiii", coordinate: coordinate)
                                                            
self.MyMapView.addAnnotation(self.pin)

In the viewcontroller I have:

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: "kite")
    }

    return annotationView
}

I made another swift file called annotation which contains:

class AnnotationPin:  NSObject, MKAnnotation {
let coordinate: CLLocationCoordinate2D
let title: String?
let subtitle: String?

init(title:String, subtitle: String, coordinate: CLLocationCoordinate2D) {
    self.title = title
    self.subtitle = subtitle
    self.coordinate = coordinate
    
    super.init()
 }}

I already called this class for subtitles and titles and I suppose I can call it again to choose my Image. But I don't know how to do it, and I don't find answers on other topics.


Solution

  • You can add an image property to your AnnotationPin class and then in viewForAnnotation you use a conditional downcast to see if you are dealing with one of your annotations. If you are then you can use the image property

    class AnnotationPin:  NSObject, MKAnnotation {
    let coordinate: CLLocationCoordinate2D
    let title: String?
    let subtitle: String?
    let image: UIImage?
    
    init(title:String, subtitle: String, image: UIImage, coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.subtitle = subtitle
        self.coordinate = coordinate
        self.image = image
        super.init()
     }}
    
    
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        
        guard let myAnnotation = annotation as? AnnotationPin  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 = myAnnotation.image
        }
    
        return annotationView
    }