从Firebase检索数据并作为注释存储在地图上 [英] Retrieving data from Firebase and storing as annotations on a map
本文介绍了从Firebase检索数据并作为注释存储在地图上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经成功地将数据从我在视图控制器中创建的表单存储到Firebase数据库中,但是现在需要检索数据以在第二个视图控制器上的地图上显示为批注,但我不知道该怎么做在网上搜索并没有找到任何东西后,继续执行此任务.
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.
这是我的工作代码,可将我的数据成功保存到Firebase数据库:
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)
}
}
推荐答案
在完成此工作之后,我最终通过使用以下函数并在viewDidLoad()中调用了它来使其工作
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)
})}
这篇关于从Firebase检索数据并作为注释存储在地图上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文