不断从云端firestore读取坐标并随着坐标变化更新地图上的点 - Swift, apple mapkit

Reading a coordinate from the cloud firestore continously and updating the point on the map as the coordinate changes - Swift, apple mapkit

问题陈述:

我目前正在开发一个应用程序,可以跟踪我大学里的所有公交车。每辆公交车都配备了一个 GPS 模块,每 5 秒向云 fireStore 发送更新的坐标和行进路线。在应用程序方面,我想在更新的坐标发生变化时读取它。

云 Firestore:

busCurrentLoc(如图所示)中有不同的公交车号码,每个公交车号码都有各自的经度和纬度以及标题。

应用端:

用户可以在屏幕上select他想跟踪哪辆公交车。当他 selects 公交车号码时,他会看到地图屏幕,其中公交车的实时位置每 5 秒更新一次。我已经完成了 selection 过程。 Real-Time 跟踪是问题所在。

我在下面的代码中包含了关于每个变量的必要注释。 我面临的问题是每当云 firestore 发生变化时更新 currentLocation 变量。

代码:

    //The mapview outlet
    @IBOutlet var mapView: MKMapView!

    //The variable which stores the current coordinates of the bus from cloud firebase
    var currentLocation = [String: CLLocationCoordinate2D]()

    //This variable is assigned from the previous ViewController during segue.
    var selectedNum: String 

    let db = Firestore.firestore()

       override func viewDidLoad() {
        super.viewDidLoad()

        mapView.delegate = self

        //Function to get the coordinate from cloud firestore
        getCoordinates()
    }


//MARK: - Get coordinates
    /**
     Function to get coordinates from the Cloud firestore and store it in the global "currentLocation" variable.

     */
    func getCoordinates() {

        db.collection("busCurrentLoc").getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {
                    if document.documentID == self.selectedNumber {
                        let latitude = document.data()["latitude"]
                        let longitude = document.data()["longitude"]
                        let coord: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude as! CLLocationDegrees, longitude: longitude as! CLLocationDegrees)
                        self.currentLocation = ["Location": coord]

                        // prints : ["Location": __C.CLLocationCoordinate2D(latitude: 12.89307834, longitude: 80.1411217)]
                        print(self.currentLocation)
                        self.DrawBus()
                    }
                }
            }
        }
     //This function should draw a bus/annotation on the currentLocation.
     DrawBus(at: currentLocation)
    }


//MARK: - Draw bus at currentLocation
    /**
     Function to Draw a bus at currentLocation.
     - Parameters:
     currentLocation

     */
    func drawBus(coordinate at: [String: CLLocationCoordinate2D]()) {
        let annotations = MKPointAnnotation()
        annotations.title = "Bus Location"
        annotations.coordinate = coordinate["Location"] ?? CLLocationCoordinate2D(latitude: 0, longitude: 0)
        mapView.addAnnotation(annotations)
    }

注释已添加到屏幕并显示。但是当我尝试更改 Cloud firestore 中的坐标时,坐标不会更改或注释不会更新。请帮我解决这个问题。

我设法通过使用 addSnapshotListener 而不是 getDocument 解决了这个问题

db.collection("busCurrentLoc").addSnapshotListener{ (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {
                    if document.documentID == self.selectedNumber {
                        let latitude = document.data()["latitude"]
                        let longitude = document.data()["longitude"]
                        self.currentHeading = document.data()["heading"] as! CGFloat
                        let coord: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude as! CLLocationDegrees, longitude: longitude as! CLLocationDegrees)
                        self.currentLocation = ["Location": coord]
                        print(self.currentLocation)
                        self.DrawBus()
                    }
                }
            }
        }