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

Ios 如何在选择注释时标记地图中的注释并获取标记值,ios,swift,swift3,Ios,Swift,Swift3,我正在做一个通过地图在线运送食物的项目。因此,用户应该知道厨师的位置。当用户点击注释时,它应该查看厨师的食物菜单。因此,当用户点击时,我需要调用cooks id。我可以根据标记值调用cooks id **Juntos.swift** func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { var annotationView = mapView.d

我正在做一个通过地图在线运送食物的项目。因此,用户应该知道厨师的位置。当用户点击注释时,它应该查看厨师的食物菜单。因此,当用户点击时,我需要调用cooks id。我可以根据标记值调用cooks 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
}

像这样把标签放在这里-

annotationView.tag = 1
return annotationView
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)
{
    // get annotation tag here
       let tag = view.tag
}
然后像这样得到标签-

annotationView.tag = 1
return annotationView
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)
{
    // get annotation tag here
       let tag = view.tag
}

子类MKAnnoation并使其能够保存自定义数据。。。e、 g.身份证:

class MyAnnotation: MKAnnotation {
    var identifier: String!
}
现在添加它,而不是MKAnnotation

let a = MyAnnotation()
....
a.identifier = "id525325"
mapView.addAnnotation(a)
然后在以后使用它

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

id没有将数据放入视图id中——它以某种方式“隐藏”了一个事实,即它是与UI无关的数据(但这有点自以为是),但存在一个问题。我有多个注释,因此我根据字符串变量给出标记值,但所有注释都将其标记设置为字符串的最后一个值。标记应为int值而不是string值。是的,它是int值而不是string。实际上,注释根据纬度和经度值显示在地图视图上。现在,我需要在与注释标记对应的indexpath处传递数组字符串。。例如我有一个数组。让myArray=[“q”、“w”、“e”]注释是您的dataovject,坐标旁边可以获取所有内容如果我点击标签值为2的注释,则应传递值myArray[2],即“e”,保存字符串数组,而不是您案例中的字符串