【问题标题】:Retrieving data from Firebase and storing as annotations on a map从 Firebase 检索数据并作为注释存储在地图上
【发布时间】:2017-06-08 11:29:07
【问题描述】:

我已成功地将数据从我在视图控制器中创建的表单存储到我的 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)

    }

}

【问题讨论】:

  • 搜索更多...此问题至少每 2 天出现一次...
  • @VladPulichev:你说得对,这个问题每隔几天就会出现一次。我真的想知道某个地方是否有一个代码实验室可以让开发人员走到这一步,然后让他们陷入困境。但是,如果您找到之前的那些问题(已回答的问题)并将其标记为重复,则会更有帮助。
  • 我已经搜索了作为注释的输出,但什么也没看到你有想要分享的链接吗?

标签: ios swift firebase-realtime-database annotations mapkitannotation


【解决方案1】:

在处理完这个之后,我最终通过使用以下函数并在 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)

        })}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 2017-05-26
    • 2020-10-23
    • 2017-08-30
    • 1970-01-01
    相关资源
    最近更新 更多