查找选定的 MapKit Annotation 的 postID
Find selected MapKit Annotation's postID
我正在尝试在 didSelect 上设置所选 MapKit 的注释 ID(在我的例子中为 mapEventID),并在 Swift 准备转场时使用:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let postViewVC = segue.destination as! PostView
postViewVC.eventID = mapEventID
}
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)
}
}
}
}
问题是它总是使用最近添加的 post 的 ID 作为 mapEventID,而不是用户实际点击的 ID。这是有道理的,因为当通过观察者从 Firebase 检索 ID 时,所有键的 'observes' 和最近的一个成为 ID。
如何在用户点击注释时获取 specific mapEventID?目前我有:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
mapEventID = //How do I change the EventID here?
}
我希望我可以在 Geofire 观察器中将 ID 添加到注释的描述'属性,但是 Swift 没有提供任何语法:
let annotation = EventAnnotation (coordinate: coordinate, withKey: eventID as! String, title: postTitle, subtitle: eventDetails)
试试下面的代码:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let annotation = view.annotation as? EventAnnotation {
mapEventID = annotation.eventKey
}
}
我正在尝试在 didSelect 上设置所选 MapKit 的注释 ID(在我的例子中为 mapEventID),并在 Swift 准备转场时使用:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let postViewVC = segue.destination as! PostView
postViewVC.eventID = mapEventID
}
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)
}
}
}
}
问题是它总是使用最近添加的 post 的 ID 作为 mapEventID,而不是用户实际点击的 ID。这是有道理的,因为当通过观察者从 Firebase 检索 ID 时,所有键的 'observes' 和最近的一个成为 ID。
如何在用户点击注释时获取 specific mapEventID?目前我有:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
mapEventID = //How do I change the EventID here?
}
我希望我可以在 Geofire 观察器中将 ID 添加到注释的描述'属性,但是 Swift 没有提供任何语法:
let annotation = EventAnnotation (coordinate: coordinate, withKey: eventID as! String, title: postTitle, subtitle: eventDetails)
试试下面的代码:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let annotation = view.annotation as? EventAnnotation {
mapEventID = annotation.eventKey
}
}