0

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

Little Helper
  • 1,870
  • 3
  • 12
  • 20
Marc75
  • 27
  • 5
  • have you tried the special symbol of new line "\n" and append the string on the annotation title string . – Anil Kumar Aug 10 '20 at 08:15
  • I don't see the connection with adding the title below the annotation. I would like to see the title under the annotation on the map display and keep the calloutAccessory to access my landing page – Marc75 Aug 10 '20 at 08:22

1 Answers1

0

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.

Gerd Castan
  • 6,275
  • 3
  • 44
  • 89
  • Important addition: set `glyphText = ""` and `markerTintColor = .clear` on the `MKMarkerAnnotationView` to prevent it showing a pin/cluster glyph over your own `image` (Spotted in https://stackoverflow.com/a/51389008) – Michael Feb 16 '22 at 01:55