gpt4 book ai didi

ios - 查找选定的 MapKit Annotation 的 postID

转载 作者:行者123 更新时间:2023-11-28 14:01:58 25 4
gpt4 key购买 nike

我正在尝试在 didSelect 上设置所选 MapKit 的注释 ID(在我的例子中为 mapEventID),并在 Swift 准备转场时使用:

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

ma​​pEventID 在 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)
}
}
}
}

问题是它总是使用最近添加的帖子的 ID 作为 mapEventID 而不是用户实际点击的那个。这是有道理的,因为当通过观察者从 Firebase 检索 ID 时,它会“观察”所有键,而最近的一个成为 ID。

如何在用户点击注释时获取特定 mapEventID?目前我有:

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

我希望我可以将 ID 添加到 Geofire 观察器中注释的 Description' 属性,但 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
}
}

关于ios - 查找选定的 MapKit Annotation 的 postID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53476321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com