我正在改变我发布和检索 firebase CLLocationCoordinated2D 的方式,从每个值一个帖子到一个包含所有值的帖子,所以我找到了这篇文章,我想在我自己的代码中实现它。从 Firebase 检索数据并存储为地图上的注释。 我在常量日期、时间、纬度、经度和描述上遇到了标题中提到的错误。 我还在学习Firebase,所以任何解释都会非常有帮助。这是我收到错误的函数。
func displayAnnotations() {
let ref = Database.database().reference()
ref.child("Sightings").observe(.childAdded, with: { (snapshot) in
let date = (snapshot.value as AnyObject?)!("Date") as! String?
let time = (snapshot.value as AnyObject)!("Time") as! String?
let latitude = (snapshot.value as AnyObject)!("Latitude") as! String?
let longitude = (snapshot.value as AnyObject?)!("Longitude") as! String?
let desc = (snapshot.value as AnyObject?)!("Description") as! String?
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: (Double(latitude!))!, longitude: (Double(longitude!))!)
annotation.title = date
annotation.subtitle = time
self.mapView.addAnnotation(annotation)
})}
这是发布功能:
func post() {
let date = dateLabel.text
let time = timeLabel.text
let latitude = latitudeLabel.text
let longitude = longitudeLabel.text
let sightingDescription = descriptionLabel.text
let post: [String:String] = ["Date" : date as AnyObject,
"Time" : time as AnyObject,
"Latitude" : latitude as AnyObject,
"Longitude" : longitude as AnyObject,
"Description" : sightingDescription as AnyObject]
var ref: DatabaseReference!
ref = Database.database().reference()
ref.child("Sightings").childByAutoId().setValue(post)
}
仅仅是因为它是为 swift 4.1 之前的版本编写的吗?
您将[]
更改为()
let dic = snapshot.value as! [String:String]
let date = dic["Date"]
let time = dic["Time"]
let latitude = dic["Latitude"]
let longitude = dic["Longitude"]
let desc = dic["Description"]