Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从Firebase检索数据并作为注释存储在地图上_Ios_Swift_Firebase Realtime Database_Annotations_Mapkitannotation - Fatal编程技术网

Ios 从Firebase检索数据并作为注释存储在地图上

Ios 从Firebase检索数据并作为注释存储在地图上,ios,swift,firebase-realtime-database,annotations,mapkitannotation,Ios,Swift,Firebase Realtime Database,Annotations,Mapkitannotation,我已成功地将在视图控制器中创建的表单中的数据存储到Firebase数据库中,但现在需要检索数据以在第二个视图控制器上的地图上显示为注释,但不知道在联机搜索后如何执行此任务 以下是成功将数据保存到Firebase数据库的工作代码: import UIKit import CoreLocation import Firebase class AddSightingViewController: UIViewController { @IBOutlet weak var dateLabel

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

    }

}

在完成这项工作之后,我最终通过使用以下函数并在viewDidLoad()中调用它使其工作


搜索更多。。。这个问题至少每两天出现一次。@VladPulichev:你是对的,这个问题每隔几天就会出现一次。我真的很想知道是否有一个代码实验室能让开发人员走到这一步,然后让他们束手无策。但如果您找到以前的问题(已回答的问题)中的一个,并将其标记为副本,则会更有帮助。我已搜索了作为注释的输出,但没有看到任何内容。您是否有要共享的链接?
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)

        })}