0

When MKMarkerAnnotationView is used for annotation, the title is shown under the icon, how to do it in MKAnnotationView?

Example:

title show in under icon:

annotationView = MKMarkerAnnotationView(annotation: nil, reuseIdentifier: identifier)

title not show in under icon:

annotationView = MKAnnotationView(annotation: nil, reuseIdentifier: identifier)
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard annotation is MKPointAnnotation else { return nil }

        
        let identifier = "Annotation"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

        
        if annotationView == nil {
            annotationView = MKMarkerAnnotationView(annotation: nil, reuseIdentifier: identifier)
          
   
            annotationView!.isEnabled = true
            annotationView!.canShowCallout = true
            annotationView!.annotation = annotation
            annotationView!.displayPriority = .required

 
        } else {
           
            annotationView!.annotation = annotation
          
        }
  • `MKAnnotationView` won't automatically do that. You'll have to do that yourself (e.g. see https://stackoverflow.com/a/30415714/1271826 for ObjC example). FYI, the `MKMarkerAnotationView` has other custom behaviors (when you select an annotation, the annotation view is enlarged, the annotation's subtitle is viewed, etc.), so you'll have to figure out how many of these custom behaviors you want to reproduce. But `MKAnnotationView` won't do any of that for you: You'll have to write all that code yourself. – Rob Sep 20 '20 at 21:52
  • I just want only a title under the annotation image. Another question: is it possible to change the image in MKMarkerAnnotation? – arttogamerpm Sep 21 '20 at 05:41
  • You can set the [`glyphImage`](https://developer.apple.com/documentation/mapkit/mkmarkerannotationview/2873823-glyphimage) of a `MKMarkerAnnotationView`. – Rob Sep 21 '20 at 13:49
  • See edit my code. Im give error: Value of type 'MKAnnotationView?' has no member 'glyphImage' – arttogamerpm Sep 21 '20 at 14:03
  • Yes, you’ve declared `annotationView` to be a `MKAnnotationView`, so it doesn’t know about the `MKMarkerAnnotationView` properties. Change it to be a `MKMarkerAnnotationView` (and cast the result of `dequeueReusableAnnotationView`). – Rob Sep 21 '20 at 14:37
  • Unrelated, but what iOS versions are you targeting? If you’re targeting iOS 11 and later, you can simplify your code a bit by using newer API. – Rob Sep 21 '20 at 14:41
  • See https://stackoverflow.com/a/63027219/1271826 for example of `MKMarkerAnnotationView` with custom rendered image (or you can use existing image asset if you want). – Rob Sep 21 '20 at 14:54
  • Yes im targeting IOS > 11 – arttogamerpm Sep 21 '20 at 14:55
  • Then you don't need to implement `viewFor` method at all. Just define your `MKMarkerAnnotationView` subclass, put all the annotation customization code there, and then you can `register` it like shown at the end of [that other answer](https://stackoverflow.com/a/63027219/1271826). – Rob Sep 21 '20 at 15:15

0 Answers0