【问题标题】:Real time transport tracking using firebase real time database in swift 4在swift 4中使用firebase实时数据库进行实时运输跟踪
【发布时间】:2019-04-25 17:05:03
【问题描述】:

如何像 ola 和 uber 一样实时跟踪运输。在我的应用程序中有两种类型的用户驱动程序,用户。我想连续跟踪司机的位置并在谷歌地图上实时显示给用户。我正在使用 swift 4.2 xocode 10.1。任何文档或任何指导都会很重要。

我已经安装了谷歌地图,并指出了出发地点和下车地点,并使用

绘制了一条折线路线

https://maps.googleapis.com/maps/api/directions/json?origin这个api。

我还可以从 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 方法。

我阅读了一些关于实时跟踪的文章文档,就像我必须连续获取驱动程序的 gps 位置并将其发送到 firebase 实时数据库,我再次从用户 firebase 获取位置并且必须移动汽车图像以及用户端的位置。

我做了同样的事情,我将驱动程序位置发送到数据库,也可以获取位置,但无法继续,请帮助。提前致谢

var locationRefrence: StorageReference {
    return Storage.storage().reference().child("Locations")
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
    print("locations = \(locValue.latitude) \(locValue.longitude)")

    if mapViewObj == nil {
        mapViewObj = GMSMapView()

        let camera = GMSCameraPosition.camera(withLatitude: locValue.latitude, longitude: locValue.longitude, zoom: 16.0)
        mapViewObj = GMSMapView.map(withFrame: self.mapView.frame, camera: camera)
        mapViewObj.delegate = self
        mapViewObj.isMyLocationEnabled = true
        mapViewObj.settings.myLocationButton = true

        self.mapView.addSubview(mapViewObj)
    }

    let location = "\(locValue.latitude) \(locValue.longitude)"

    firebaseUpload(location: location)
}

func firebaseUpload(location: String) {
    let uploadLocationRef = locationRefrence.child("location")
    let locationData = location.data(using: .utf8)

    let uploadTask = uploadLocationRef.putData(locationData!, metadata: nil) { (metadata, error) in
        print(metadata ?? "No metadata")
        print(error ?? "No error")
    }

    uploadTask.observe(.progress) { (snapshot) in
        print(snapshot.progress ?? "No progress")
    }

    uploadTask.resume()
}

func fetchLocationFromFirebase() {
    let downloadLocationRef = locationRefrence.child("location")

    let downloadTask = downloadLocationRef.getData(maxSize: 1024 * 1024 * 12) { (data, error) in
        if let data = data {
            let locationStr = String(data: data, encoding: .utf8)
            print("Fetched Locations : \(locationStr ?? "Nothing fetched...")")
        }
        print(error ?? "No error")
    }

    downloadTask.observe(.progress) { (snapshot) in
        print(snapshot.progress ?? "No progress")
    }

    downloadTask.resume()
}

【问题讨论】:

  • 我删除了标题中您要求教程的部分内容。这是题外话,会让你的问题作为题外话结束
  • @Scriptable 谢谢,任何进一步的指导都会对我有很大帮助。
  • 你是如何在 firebase 中设置 location 的?
  • 我在 firebase 中制作了一个应用程序并访问了它的存储空间。我可以看到我在 firebase 中提交的数据,也可以获取它。
  • 好的,所以驱动程序发送它...用户需要获取它。

标签: ios swift google-maps firebase-realtime-database


【解决方案1】:

我认为你的问题在这里:

let downloadTask = downloadLocationRef.getData(maxSize: 1024 * 1024 * 12) { (data, error) in

我不知道你为什么要这样做?这不是首先获取数据的好方法,而且它只获取现在存在的数据并且不监控任何更新,所以我认为您需要对 Firebase 的实时数据库进行更多研究。

对于这种特殊情况,您希望在更新位置时随时更新,这样您就可以观察“位置”子节点的变化

refHandle = postRef.observe(DataEventType.value, with: { (snapshot) in
  let postDict = snapshot.value as? [String : AnyObject] ?? [:]
  // ...
})

每次更新时,这将为您提供位置表中数据的快照

参考:https://firebase.google.com/docs/database/ios/read-and-write

【讨论】:

  • 谢谢。我会更改它,但是在获取更新后如何在地图中移动标记?
  • 保留对标记的引用并根据需要更新其位置
  • 我完全错了...我将数据存储到存储中,但我必须将数据存储到数据库中。然后只有我可以得到实时的变化。感谢@Scriptable 向我展示了正确的路径。
猜你喜欢
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
  • 2017-09-08
  • 2020-10-18
  • 2018-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多