繁体   English   中英

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

[英]Check if no nearby locations with 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.  
        }

    })
}

您将要等待查询报告完成。 GeoFire文档中

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM