Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';使用Firebase(GeoFire)和Swift,我无法通过我的位置接近用户_Swift_Firebase_Google Cloud Firestore_Geofire - Fatal编程技术网

Can';使用Firebase(GeoFire)和Swift,我无法通过我的位置接近用户

Can';使用Firebase(GeoFire)和Swift,我无法通过我的位置接近用户,swift,firebase,google-cloud-firestore,geofire,Swift,Firebase,Google Cloud Firestore,Geofire,我想让所有用户靠近我自己的位置 我在location Manager didUpdateLocation函数中编写了以下代码 First I grabbed my own location as a CCLocationCoridnate2D. if let location = locations.last{ let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: locati

我想让所有用户靠近我自己的位置

我在location Manager 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为空,应用程序正在崩溃

先谢谢你
标记

此答案取决于用户数据的存储方式,但如果您将用户的documentId存储为用户uid,则只需

let uid = Auth.auth().currentUser!.uid //should safe unwrap that optional
然后

然而,如果你想得到你在问题中所做的

let uid = Auth.auth().currentUser!.uid
let usersCollection = Firestore.firestore().collection("users")
                               .whereField("uid", isEqualTo: uid)
                        
usersCollection.getDocument(completion: { documentSnapshot, error in
    if let err = error {
         print(err.localizedDescription)
         return
     }

     guard let docs = documentSnapshot?.documents else { return }

     let thisUserUid = docs[0].documentID
     print(thisUserUid)
})
但同样,这有点多余,因为它表示将uid同时存储在documentId和子字段中,这是不必要的:

users
  uid_0 //the documentId
     name: "Users name"
     uid: "uid_0" //not needed
问题似乎是实际获取中心点-例如,如果未存储中心点,则在读取时,中心点将为空,您将得到该错误

因此,您必须首先存储它

geoFire.setLocation(CLLocation(latitude: 37.7853889, longitude: -122.4056973), forKey: uid) { (error) in
  if (error != nil) {
    print("An error occured: \(error)")
  } else {
    print("Saved location successfully!")
  }
}
一旦成功存储了中心点(您的位置),然后检索到它,那么实际的查询应该是

let center = CLLocation(userLocation)
geoFire.queryAtLocation(center, withRadius: 20);
哦,非常重要的一点是,设置位置是异步的;服务器存储数据需要时间。您应该真正处理闭包中的数据

geoFire.setLocation(CLLocation(latitude: 37.7853889, longitude: -122.4056973), forKey: uid) { (error) in
   // data is now valid so perform your query
}

但问题是如何在一段距离附近抓取用户,而不是如何从另一个地方抓取用户database@Markus问题是,如果我现在想用我的实际用户(userdocid)替换“test”,它将不起作用。因此,由于NSArray为空,应用程序正在崩溃。因此,必须首先解决这一问题。我们不知道应用程序在哪里崩溃,因为问题中没有指出故障排除;所以我们在猜测这一点。还可以查看更新的答案。@Markus我在完成答案之前已经点击了save。我添加了一些可能有用的附加信息。我不想自己设置位置。我想使用CC位置管理器。感谢所有在CC位置管理器内的人…抓住我自己的位置,然后在此时设置它
geoFire.setLocation(CLLocation(latitude: 37.7853889, longitude: -122.4056973), forKey: uid) { (error) in
   // data is now valid so perform your query
}