use*_*470 6 ios firebase geofire firebase-realtime-database
我一直在尝试使用Geofire for iOS,但似乎无法找到任何方法在循环查询中返回搜索位置的距离.GFQueryResultBlock仅返回键和位置.我是否正确地假设我必须自己计算距离?
假设我正在使用Firebase制作附近的餐馆应用程序,并希望向用户显示最近的20家餐厅,并根据它们的距离来排序.我可以创建一个圆形查询,然后通过循环增加搜索半径,直到找到20家餐馆.然后计算每个距离并在将它们显示给用户之前对它们进行排序.这是一种合理的方法,因为在应用程序中进行了大量工作来构建数据(计算距离和排序)?
我注意到javascript Geofire查询返回距离中心的距离,但我猜iOS和Android版本与此不同.
当您从 Geofire 查询时,相关结果会自动按升序排列。所以为了得到距离,我只是使用 distanceFromLocation 函数:这是我的代码:
func getGeoFirePlaces(){
let geofireRef = FIRDatabase.database().reference().child("testForGeofire")
let geoFire = GeoFire(firebaseRef: geofireRef)
//geoFireRef is pointing to a firebase reference where I previously set all places' location
let userPosition = CLLocation(latitude: 32.0776067, longitude: 34.78912)
let circleQuery = geoFire.queryAtLocation(userPosition, withRadius: 2)
circleQuery.observeEventType(.KeyEntered, withBlock: { (key: String!, location: CLLocation!) in
print("Key '\(key)' entered the search area and is at location '\(location)'")
//getting distance of each Place return with the callBack
let distanceFromUser = userPosition.distanceFromLocation(location)
print(distanceFromUser)
})
}
Run Code Online (Sandbox Code Playgroud)
希望这有帮助!
| 归档时间: |
|
| 查看次数: |
1984 次 |
| 最近记录: |