使用GeoFire检查附近是否有位置

时间:2016-05-31 02:48:44

标签: ios swift firebase firebase-realtime-database geofire

我正在尝试使用GeoFire搜索附近注册的位置,如果指定半径内没有,则返回默认值。我的问题是,当GeoFire没有在附近找到任何东西时,它绝对没有返回任何东西。有没有不同的方法来做到这一点?

    let geoFire = GeoFire(firebaseRef: DataService().LOC_REF)
    let myLoc = CLLocation(latitude: 10.500000, longitude: -100.916664)
    let circleQuery = geoFire.queryAtLocation(myLoc, withRadius: 100.0)
    circleQuery.observeEventType(GFEventType.KeyEntered, withBlock: {
        (key: String!, location: CLLocation!) in
        self.allKeys[key]=location
        print(self.allKeys.count) //NOT GETTING HERE
        print(key)
        //from here i'd like to use the key for a different query, or determine if there are no keys nearby
            }
        })
    })

提前致谢

2 个答案:

答案 0 :(得分:4)

这就是你可以使用这两个函数来构建一个键列表,并在列表完成创建时监听。

func fetchLocations() {
    keys = [String]()

    let geoFire = GeoFire(firebaseRef: DataService().LOC_REF)
    let myLoc = CLLocation(latitude: 10.500000, longitude: -100.916664)
    let circleQuery = geoFire.queryAtLocation(myLoc, withRadius: 100.0)

    // Populate list of keys
    circleQuery.observeEventType(.KeyEntered, withBlock: { (key: String!, location: CLLocation!) in
        print("Key '\(key)' entered the search area and is at location '\(location)'")
        keys.append(key)
    })

    // Do something with list of keys.
    circleQuery.observeReadyWithBlock({
        print("All initial data has been loaded and events have been fired!")
        if keys.count > 0 {
            // Do something with stored fetched keys here.
            // I usually retrieve more information for a location from firebase here before populating my table/collectionviews.  
        }

    })
}

答案 1 :(得分:1)

您需要等待查询报告已完成。来自the GeoFire documentation

  

有时您想知道何时从服务器加载了所有初始密钥的数据,并且已经触发了这些密钥的相应事件。例如,您可能希望在数据完全加载后隐藏加载动画。 GFQuery提供了一种监听这些就绪事件的方法:

query.observeReadyWithBlock({
  println("All initial data has been loaded and events have been fired!")
})
相关问题