【问题标题】:Check if no nearby locations with GeoFire使用 GeoFire 检查附近是否没有位置
【发布时间】:2016-09-28 21:34:32
【问题描述】:

我正在尝试使用 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
            }
        })
    })

提前致谢

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢.. 查询完成后如何访问找到的密钥?
    • 当它们进入 key_entered 处理程序时,您必须捕获它们。
    • 我可以只查询已经在里面的吗?我的位置不是动态的。
    • 是的。你observeEventType(.KeyEntered 并在它们进来时处理它们。当observeReadyWithBlock 触发时,你就完成了。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 2014-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多