[Swift]注释视图如何始终显示标注

[Swift]How an annotationview always show callout

我想显示 annotationviewcallout 始终显示,我的意思是当加载 mapview 时,显示所有注释的 callout

我该怎么做?

任何时候,地图上都只能有一个标注。当要呈现新的标注时,框架会重复使用以前的标注,这里显示的是 添加注释时的单个 标注视图,就像这样

let annotation = MKPointAnnotation()
annotation.title = "Title for Callout"
mapView.addAnnotation(annotation)

这会导致委托方法 viewForAnnotation 被触发:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let view = MKAnnotationView(annotation: annotation, reuseIdentifier: "AnnotId")
    view.canShowCallout = true
    return view
}

框架将添加上面返回的视图并触发 didAddViews 委托,现在通过选择注释显示标注视图,就像这样

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
    if let annotation = views.first(where: { [=12=].reuseIdentifier == "AnnotId" })?.annotation {
        mapView.selectAnnotation(annotation, animated: true)
    }
}
  1. 在地图上添加注释:mapView.addAnnotation(myAnnotation)
  2. Select注释mapView.selectAnnotation(myAnnotation, animated: false)
  3. 使用代码 if let myAnnotation = mapView.annotations.first(where: { [=13=] is MKPointAnnotation }) { mapview.selectAnnotation(myAnnotation, animated: false) }
  4. 添加私有函数 private func bringMyAnnotationToFront()
  5. 设置 MKMapViewDelegate 并使用 bringMyAnnotationToFront 实现两个方法: func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)func mapView(MKMapView, didDeselect: MKAnnotationView)