如何将按钮添加到注释,当前解决方案不起作用

How to add a button to an annotation, current solution not working

我正在尝试将一个按钮添加到我放置的引脚上,这是我在 Whosebug 上找到的当前解决方案,下面 post 的 link,它不起作用,我只是获取标准图钉

我试过:

import UIKit
import MapKit
import CoreLocation
import FirebaseAuth
import FirebaseDatabase


class map: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var map: MKMapView!
    let locationManager = CLLocationManager()


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        displayPins()
    }
    func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
        if control == view.rightCalloutAccessoryView{
            print(view.annotation?.title) // annotation's title


            //Perform a segue here to navigate to another viewcontroller
            // On tapping the disclosure button you will get here
        }
    }

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

        print("viewForannotation")
        if annotation is MKUserLocation {
            //return nil
            return nil
        }

        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView

        if pinView == nil {
            //println("Pinview was nil")
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
        }

        var button = UIButton(type: UIButton.ButtonType.detailDisclosure) as UIButton // button with info sign in it

        pinView?.rightCalloutAccessoryView = button


        return pinView
    }






    func displayPins(){
        var ref: DatabaseReference!

        ref = Database.database().reference()


        ref.child("/geo").observe(.childAdded, with: { (snapshot) in



            let annotation = MKPointAnnotation()
            let userID = (snapshot.value as AnyObject?)!["userID"] as! String?
            let latitude = (snapshot.value as AnyObject?)!["lat"] as! String?
            let longitude = (snapshot.value as AnyObject?)!["long"] as! String?

            let location = CLLocation(latitude: Double(latitude!) as! CLLocationDegrees, longitude: Double(longitude!) as! CLLocationDegrees  )

//            let geocoder = CLGeocoder()
//            geocoder.reverseGeocodeLocation(location) { (placemark, error) in
//                print(placemark!)
//
//            }
            ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
                // Get user value


                let value = snapshot.value as? NSDictionary
                let name = value?["fullname"] as? String ?? ""

                let annotationView = MKAnnotationView()
                let anLocation = CLLocationCoordinate2D(latitude: Double(latitude!) as! CLLocationDegrees, longitude: Double(longitude!) as! CLLocationDegrees)
                annotation.coordinate = anLocation
                annotation.title = name



                self.map.addAnnotation(annotation)
            }) { (error) in
                print(error.localizedDescription)
            }




        })







    }

    @IBAction func addListing(_ sender: Any) {

        self.performSegue(withIdentifier:  "listing", sender: nil)

    }








}

您需要添加代表MKMapViewDelegate。并使用函数,

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

}