Add marker label on Apple Maps in Swift

apple-maps, ios, swift, swift3

Solution

You can try something like this putting the `UILabel` over an image:

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

    // Leave default annotation for user location
    if annotation is MKUserLocation {
      return nil
    }

    let reuseID = "Location"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseID)
    if annotationView == nil {
      let pin = MKAnnotationView(annotation: annotation,
                                 reuseIdentifier: reuseID)
          pin.image = UIImage(named: "cabifyPin")
          pin.isEnabled = true
          pin.canShowCallout = true

      let label = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
          label.textColor = .white
          label.text = annotation.id // set text here
          pin.addSubview(label)

      annotationView = pin
    } else {
      annotationView?.annotation = annotation
    }

    return annotationView
  }

Problem

Based on this it's possible to add marker label/badge on Google Maps for iOS - Add text on custom marker on google map for ios Does anyone knows how to do it for Apple Maps? I need something like this:

Original source

Related problems