如何在地图中标记注释并在选择注释时获取标记值

Wid*_*ogy 4 ios swift swift3

我正在做一个通过地图在线送餐的项目。所以用户应该知道厨师的位置。当用户点击注释时,它应该查看厨师的食物菜单。所以当用户点击我需要调用厨师 ID。我可以根据标签值调用厨师ID。

**Juntos.swift**


func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> 
MKAnnotationView?
{
    var annotationView = 
mapView.dequeueReusableAnnotationView(withIdentifier: "identifier")

    if annotationView == nil
      {
        annotationView = MKAnnotationView(annotation: annotation, 
         reuseIdentifier: "identifier")
        annotationView!.canShowCallout = true
      }
    else
      {
        annotationView?.annotation = annotation
      }

    guard !(annotation is MKUserLocation) else
      {
        return nil
      }

    if iamA == "COOK"
      {
        annotationView!.image = UIImage(named: "foodie")
      }
    else
      {
        annotationView!.image = UIImage(named: "cook")
      }
           return annotationView
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)
{
    //    how can i get annotation tag here
}
Run Code Online (Sandbox Code Playgroud)

Dai*_*jan 5

子类 MKAnnoation 并启用它来保存自定义数据......例如id:

class MyAnnotation: MKAnnotation {
    var identifier: String!
}
Run Code Online (Sandbox Code Playgroud)

现在添加它而不是 MKAnnotation

let a = MyAnnotation()
....
a.identifier = "id525325"
mapView.addAnnotation(a)
Run Code Online (Sandbox Code Playgroud)

然后在以后使用它

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    guard let a = view.annoation as? MyAnnoation else {
        return
    }
    let identifier = a.identifier
Run Code Online (Sandbox Code Playgroud)