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

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

提前致谢

最佳答案

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

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

    })
}

关于ios - 使用 GeoFire 检查附近是否没有位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37536091/

相关文章:

SWIFT:控制另一个用户设备上的 View

ios - void 函数中出现意外的非 void 返回值 - Swift 4(Firebase、Firestore)

firebase - Flutter Firebase已弃用的api,无法在发行版中构建Appbundle

reactjs - 未捕获的语法错误 : Unexpected token '<' while deploying create react app to firebase hosting

iphone - 当内存警告不足时,通过界面构建​​器添加的 View 会发生什么情况?

ios - 使用自定义标题按钮修改 TableView 中的部分?

arrays - 如何在 Swift 4.0 中使用 WritableKeyPath 更新数组

firebase - 新的 Firebase Firestore DocumentDb 如何对大型子集合建模

ios - 检测应用程序中是否存在 WatchKit 扩展

ios - 定位一个UI Image "In Between "UI Label的文本