为什么我的MKAnnotation没有显示按钮?

时间:2017-06-12 05:51:22

标签: swift swift3 mkannotation mkannotationview

以下是我的代码:

class SearchRunViewController: UIViewController, MKMapViewDelegate {

let databaseRef = FIRDatabase.database().reference()
var databaseHandle: UInt!
let regionRadius: CLLocationDistance = 1000
var runs = Array<RunDetail>()
var runName: String?
var distance: Int?
var time: Int?
var user: String?
var count: Int!

@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var distanceLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    nameLabel.isHidden = true
    distanceLabel.isHidden = true
    timeLabel.isHidden = true

}

override func viewDidLoad() {
    super.viewDidLoad()
    let initialLocation = CLLocation(latitude: 37.3318, longitude: -122.0312)
    centerMapOnLocation(initialLocation)

    loadAnnotations { (runs) in

        self.mapView.addAnnotations(runs)

    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

func centerMapOnLocation(_ location: CLLocation) {
    let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
                                                              regionRadius * 2.0, regionRadius * 2.0)
    mapView.setRegion(coordinateRegion, animated: true)
}

func loadAnnotations(completion: @escaping (Array<RunDetail>) -> Void) {

    databaseHandle = databaseRef.child("RunList").observe(.value, with: { (snapshot) in

        self.count = Int(snapshot.childrenCount)

        for child in snapshot.children.allObjects as! [FIRDataSnapshot]  {

            let childDict = child.value as? [String: Any]
            let name = childDict?["name"] as? String
            let rundistance = childDict?["distance"] as? Int
            let runtime = childDict?["time"] as? Int
            let runuser = childDict?["user"] as? String
            let runlat = childDict?["startLat"] as? Double
            let runlong = childDict?["startLong"] as? Double
            let runcoordinate = CLLocationCoordinate2D(latitude: runlat!, longitude: runlong!)

            let runDet = RunDetail(runName: name!, coordinate: runcoordinate, user: runuser!, distance: rundistance!, time: runtime!)
            self.runs.append(runDet)

        }
        completion(self.runs)

    })

}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let annotationIdentifier = "run"
    var view = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)
    if view == nil {
        view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        view?.canShowCallout = true
        view?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    } else {
        view?.annotation = annotation
    }
    return view
}

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    nameLabel.isHidden = false

}


}

我的RunDetail类:

class RunDetail: NSObject, MKAnnotation {

let runName: String?
let coordinate: CLLocationCoordinate2D
let user: String
let distance: Int
let time: Int

init(runName: String, coordinate: CLLocationCoordinate2D, user: String, distance: Int, time: Int)   {

    self.runName = runName
    self.coordinate = coordinate
    self.user = user
    self.distance = distance
    self.time = time

    super.init()
}

var title: String?   {

    return runName

}

var subtitle: String?   {

    return ("Distance: \(distance)m in \(time) seconds, completed by \(user)")

}

func mapItem() -> MKMapItem {
    let placemark = MKPlacemark(coordinate: coordinate)

    let mapItem = MKMapItem(placemark: placemark)
    mapItem.name = runName

    return mapItem
}

}

我相信我已经设置了所有内容,因为按钮可以显示我的注释(注释显示在地图上,而不是带有注释的按钮)。我做错了什么?

0 个答案:

没有答案