How to add title under annotation?

546 Views Asked by At

I managed to make my annotations with a personalized image by annotation and the title in the popup. My question: Is it possible to also add the title below the annotation? Thank you in advance for your feedback.

The view controller

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        let pageDecouverte = listeDesPoints[view.tag]
        selectedLieuDecouverte = pageDecouverte
        self.performSegue(withIdentifier: "showPageDecouverte", sender: nil)
    }
    
    
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
    {
        if !(annotation is MKPointAnnotation) {
            return nil
        }
        
        let annotationIdentifier = "location"
        var annotationView = carteRegion.dequeueReusableAnnotationView(withIdentifier: (annotationIdentifier))
        
        
        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
            annotationView!.canShowCallout = true
        }
        else {
            annotationView!.annotation = annotation
        }
        
        if annotation is MyAnnotation {
            let imageName = (annotation as! MyAnnotation).imageName
            annotationView?.tag = (annotation as! MyAnnotation).tag
            if imageName != nil{
                let interetImage = UIImage(named: imageName!)
                annotationView!.image = interetImage
                
            }
            
        }
        annotationView?.clusteringIdentifier = annotationIdentifier
        annotationView?.canShowCallout = true
        annotationView?.rightCalloutAccessoryView = UIButton(type:.detailDisclosure)
        return annotationView
        
        
    }
}

For my MyAnnotation

import UIKit
import MapKit
import Parse


class MyAnnotation: MKPointAnnotation {
    
    
    var imageName: String?
    var annotationTitle: String?
    var tag : Int = 0
    var pageDecouverte:PFPageDecouverte?
}

enter image description here

1

There are 1 best solutions below

1
Gerd Castan On

Instead of MKAnnotationView use the subclass MKMarkerAnnotationView to render the title below the marker.

Also in MyAnnotation rename annotationTitle to title, then MKMarkerAnnotationView automatically knows what to render.

See the definition of protocol MKAnnotation to see why.