首页 > 解决方案 > 不断从云存储中读取坐标并随着坐标的变化更新地图上的点 - Swift,apple mapkit

问题描述

问题陈述:

我目前正在开发一个可以跟踪我大学所有公交车的应用程序。每辆公共汽车都配备了一个 GPS 模块,该模块每 5 秒发送一次更新的坐标以及前往云端 fireStore。在应用程序方面,我想在更新的坐标发生变化时读取它。

云火库:

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

此图显示 Cloud FireStore

应用端:

向用户提供了一个屏幕,他可以在其中选择他想要跟踪的公共汽车。当他选择巴士号码时,他会看到地图屏幕,其中巴士的实时位置每 5 秒更新一次。我已经完成了选择过程。实时跟踪是问题所在。

此图像表示地图视图上显示的注释

我在下面的代码中包含了关于每个变量的必要注释。我面临的问题是每当云 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 中的坐标时,坐标不会更改或注释不会更新。请帮我解决这个问题。

标签: swiftmapkitmapkitannotation

解决方案


我设法通过使用 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()
                    }
                }
            }
        }

推荐阅读