在Swift中的Apple Maps上添加标记标签

时间:2017-05-02 16:43:07

标签: ios swift swift3 apple-maps

基于此,可以在Google Maps for iOS上添加标记标签/徽章 - Add text on custom marker on google map for ios

有谁知道如何为Apple Maps做到这一点?我需要这样的东西:

enter image description here

1 个答案:

答案 0 :(得分:5)

你可以试试这样的事情,将UILabel放在图像上:

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
  }