【发布时间】:2021-02-22 19:13:57
【问题描述】:
我想让所有用户都靠近我自己的位置。
我在位置管理器 didUpdateLocation 函数中编写了以下代码。
First I grabbed my own location as a CCLocationCoridnate2D.
if let location = locations.last{ let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
然后我进行了查询以获取我所在位置附近的所有用户。
let geofireRef = Database.database().reference()
let geoFire = GeoFire(firebaseRef: geofireRef)
geoFire.setLocation(CLLocation(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude), forKey: "test")
let centerQuery = CLLocation(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let circleQuery = geoFire.query(at: centerQuery, withRadius: 5)
var queryHandle = circleQuery.observe(.keyEntered, with: { (key: String!, location: CLLocation!) in
print("Key '\(key)' entered the search area and is at location '\(location)'")
})
如果我现在想用我的实际用户 (userdocid) 替换“test”,它将无法正常工作。
我如何抓住我的用户:
Firestore.firestore().collection("users").whereField("uid", isEqualTo: Auth.auth().currentUser?.uid ?? "x")
.getDocuments { (querySnapshot, err) in
if err != nil {
print("Failed")
return
}
let userdocid = querySnapshot!.documents[0].documentID
由于 NSArray 为空,应用程序正在崩溃。
提前谢谢你 标记
【问题讨论】:
标签: swift firebase google-cloud-firestore geofire