我已成功设法从我在视图控制器中创建的表单中将数据存储到我的Firebase数据库,但现在需要检索数据以在第二个视图控制器的地图上显示为注释但不知道我应该如何在搜索并在网上找不到任何东西后继续执行此任务。
以下是我的工作代码,可将我的数据成功保存到Firebase数据库:
import UIKit
import CoreLocation
import Firebase
class AddSightingViewController: UIViewController {
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var latitudeLabel: UILabel!
@IBOutlet weak var longitudeLabel: UILabel!
@IBOutlet weak var descriptionLabel: UITextView!
var locManager = CLLocationManager()
var currentLocation: CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
// Set Date & Time
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.setLocalizedDateFormatFromTemplate("EEEE, MMM d, yyyy") // // set template after setting locale
let dateString = "\(dateFormatter.string(from: Date() as Date))"
dateLabel.text = String(dateString)
let timeFormatter = DateFormatter()
timeFormatter.timeStyle = .medium
timeFormatter.setLocalizedDateFormatFromTemplate("hhmm")
let timeString = "\(timeFormatter.string(from: Date() as Date))"
timeLabel.text = String(timeString)
// Set Latitude & Longitude
locManager.requestWhenInUseAuthorization()
if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse ||
CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways){
currentLocation = locManager.location
self.latitudeLabel.text = String("\(currentLocation.coordinate.latitude)")
self.longitudeLabel.text = String("\(currentLocation.coordinate.longitude)")
}
}
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 : AnyObject] = ["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)
}
@IBAction func saveButton(_ sender: Any) {
post()
// Create the alert controller
let alertController = UIAlertController(title: "Sighting Logged Successfully", message: "Your logged entry will now show up on the map for others to see. Thank You!", preferredStyle: .alert)
// Create the actions
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
UIAlertAction in
NSLog("OK Pressed")
}
// Add the actions
alertController.addAction(okAction)
// Present the controller
self.present(alertController, animated: true, completion: nil)
}
}
答案 0 :(得分:0)
在完成这项工作之后,我最终通过使用以下函数并在viewDidLoad()中调用它来使其工作
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.map.addAnnotation(annotation)
})}