在 SWIFT 4 中使用 Firebase 实时数据库进行实时运输跟踪



如何像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()
}

我认为您的问题就在这里:

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

最新更新