简体   繁体   English

查找选定的MapKit注释的postID

[英]Find selected MapKit Annotation's postID

I'm trying to set the selected MapKit's Annotation ID (mapEventID in my case) on didSelect and to use when Swift is preparing the segue: 我正在尝试在didSelect上设置选定的MapKit的注释ID(在我的情况下为mapEventID),并在Swift准备segue时使用:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   let postViewVC = segue.destination as! PostView
   postViewVC.eventID = mapEventID
}

mapEventID is initialized in the GeoFire observer: mapEventID在GeoFire观察器中初始化:

func retrieveAllEvents () {

    let postDB = Database.database().reference().child("DBName")

    postDB.observe(.childAdded) { (snapshot) in

        let snapshotValue =  snapshot.value as! Dictionary <String,AnyObject>
        let postTitle = snapshotValue ["EventTitle"]!
        let postLocation = snapshotValue ["VisibleLocation"]!
        let eventID = snapshotValue ["EventID"]
        let postTime = snapshotValue ["EventDateTimeStart"]!

        let date = self.convertTimestamp(serverTimestamp: postTime as! Double)
        let mapEventDetails : String  = "\(date) - \(postLocation)"

        self.geoFire.getLocationForKey(locationID! as! String) { (location, error) in
            if (error != nil) {
                print("An error occurred getting the location for eventID:", eventID as Any)

            } else if (location != nil) {

                let postLat = location?.coordinate.latitude
                let postLong = location?.coordinate.longitude
                let coordinate = CLLocationCoordinate2D(latitude: postLat!, longitude: postLong!)
                let annotation = EventAnnotation (coordinate: coordinate, withKey: eventID as! String, title: mapEventDetails, subtitle: mapEventDetails)

                self.mapEventID = annotation.eventKey
                self.eventMap.addAnnotation(annotation)

            } else {
                print("GeoFire does not contain a location for:", eventID as Any)
            }
        }
    }
}

The problem is that it always uses the most recently added post's ID as the mapEventID and not the one the user actually tapped on. 问题在于它总是使用最新添加的帖子的ID作为mapEventID,而不是用户实际使用的ID。 This makes sense because when the IDs are retrieved from Firebase through the observer it 'observes' for all the keys and the most recent one becomes the ID. 这是有道理的,因为当通过观察者从Firebase检索ID时,它会“观察”所有密钥,而最新的则成为ID。

How do I obtain the specific mapEventID when the user taps on the annotation? 当用户点击注释时,如何获取特定的 mapEventID? At the moment I have: 目前,我有:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    mapEventID = //How do I change the EventID here?
} 

I was hoping I could add the ID to the annotation's Description' property in the Geofire observer but Swift does not provide any syntax for that in: 我希望可以将ID添加到Geofire观察器的注释的Description'属性中,但是Swift在以下方面不提供任何语法:

let annotation = EventAnnotation (coordinate: coordinate, withKey: eventID as! String, title: postTitle, subtitle: eventDetails)

Try the following code: 尝试以下代码:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if let annotation = view.annotation as? EventAnnotation {
        mapEventID = annotation.eventKey
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM