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

Check if no nearby locations with GeoFire

我正在尝试使用 GeoFire 搜索附近的注册位置,如果指定半径内有 none,则 return 为默认值。我的问题是,当 GeoFire 在附近找不到任何东西时,它 return 绝对什么都没有。有其他方法吗?

    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
            }
        })
    })

提前致谢

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

Sometimes you want to know when the data for all the initial keys has been loaded from the server and the corresponding events for those keys have been fired. For example, you may want to hide a loading animation after your data has fully loaded. GFQuery offers a method to listen for these ready events:

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

这就是您如何使用这两个函数来构建键列表,并监听列表何时完成创建。

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.  
        }

    })
}