问题
I've successfully managed to store data to my Firebase database from a form I created in a view controller but now need to retrieve the data to display as annotations on a map on a second view controller but do not know how I should go about doing this task after searching and finding nothing online.
Here is my working code that saves my data successfully to the Firebase database:
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)
}
}
回答1:
After working on this I've eventually got it to work by using the following function and calling it in 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)
})}
来源:https://stackoverflow.com/questions/44434480/retrieving-data-from-firebase-and-storing-as-annotations-on-a-map