Firebase 中的新 Firestore 数据库本身支持基于位置的地理查询吗?即查找 10 英里内的帖子,或查找 50 个最近的帖子?
我看到有一些现有的实时 firebase 数据库项目,例如 geofire - 这些项目也可以适用于 firestore 吗?
更新:Firestore 目前不支持实际的 GeoPoint 查询,因此虽然以下查询成功执行,但它仅按纬度过滤,而不是按经度过滤,因此将返回许多不附近的结果。最好的解决方案是使用地理哈希 https://en.wikipedia.org/wiki/Geohash。要了解如何自己做类似的事情,请查看此video https://www.youtube.com/watch?v=mx1mMdHBi5Q.
这可以通过创建小于大于查询的边界框来完成。至于效率,我就不好说了。
请注意,应检查约 1 英里的纬度/经度偏移的准确性,但这是执行此操作的快速方法:
SWIFT 3.0版本
func getDocumentNearBy(latitude: Double, longitude: Double, distance: Double) {
// ~1 mile of lat and lon in degrees
let lat = 0.0144927536231884
let lon = 0.0181818181818182
let lowerLat = latitude - (lat * distance)
let lowerLon = longitude - (lon * distance)
let greaterLat = latitude + (lat * distance)
let greaterLon = longitude + (lon * distance)
let lesserGeopoint = GeoPoint(latitude: lowerLat, longitude: lowerLon)
let greaterGeopoint = GeoPoint(latitude: greaterLat, longitude: greaterLon)
let docRef = Firestore.firestore().collection("locations")
let query = docRef.whereField("location", isGreaterThan: lesserGeopoint).whereField("location", isLessThan: greaterGeopoint)
query.getDocuments { snapshot, error in
if let error = error {
print("Error getting documents: \(error)")
} else {
for document in snapshot!.documents {
print("\(document.documentID) => \(document.data())")
}
}
}
}
func run() {
// Get all locations within 10 miles of Google Headquarters
getDocumentNearBy(latitude: 37.422000, longitude: -122.084057, distance: 10)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)