如何将 MKPointAnnotation 变成按钮?

How to turn a MKPointAnnotation to a button?

每当用户点击 MKPointAnnotation,我希望他被重定向到特定视图。我的问题是如何获取注释以在点击时执行操作?

这是我的代码:

for i in closestpharmacyname {
                var docref2 = db.collection("pharmacies").document(i)
                print("Pharmacy: ", i)
                docref2.getDocument(source: .cache) { (document, error) in
                    if var document  = document {
                        var pharmacylatitude = document.get("latitude") as! Double
                        var pharmacylongitude = document.get("longitude") as! Double
                        print(pharmacylatitude, pharmacylongitude)
                             
                        var pharmacyannotation = MKPointAnnotation()
                        pharmacyannotation.coordinate = CLLocationCoordinate2D(latitude: pharmacylatitude, longitude: pharmacylongitude)
                        pharmacyannotation.title = i
                    
                        self.MapView.addAnnotation(pharmacyannotation)
                    } else{
                        print("Document Does not exist")
                    }
                    
                }
            }

为此,您需要添加:

yourAnnotationView?.rightCalloutView = UIButton(type: UIButtonType.detailDisclosure)

进入你的 mapView(_:viewFor:) 函数。这将改变注释的外观,但侧面会有一个按钮,您可以按该按钮来执行不同的操作 tasks.The 注释看起来像这样:

为了处理这些任务,您还需要添加一个新函数:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
  //perform tasks here
}