使用 Swift 在 Firebase 中进行 GeoHash 查询
GeoHash Queries in Firebase with Swift
我对 Firebase 中提供的新地理查询功能感到非常兴奋。我正在试用最近推出的 'GeoFire/Utils' pod。我设置了一个测试 iOS 应用程序,这里是 repo。如果有人想克隆并尝试,我会保留规则。我的 Firestore 文档如下所示。
date : January 29, 2021 at 1:29:00 PM UTC-8
geohash:"9mupwu3mkc"
id:"13101C7F-D7FF-4141-BC5A-76602173C096"
lat:33.6863622
lng:-117.8264411
ownerAddress:"1 Civic Center Plaza, Irvine CA 92606"
我正在使用来自 firebase 的示例 code。这是对 firebase
的调用
func getallDocs(radius: Double) {
// Find pickups within 50km of Basecamp
let center = CLLocationCoordinate2D(latitude: 33.9742268, longitude: -118.3947792)
let radiusInKilometers: Double = radius
// Each item in 'bounds' represents a startAt/endAt pair. We have to issue
// a separate query for each pair. There can be up to 9 pairs of bounds
// depending on overlap, but in most cases there are 4.
let queryBounds = GFUtils.queryBounds(forLocation: center,
withRadius: radiusInKilometers)
let queries = queryBounds.compactMap { (any) -> Query? in
guard let bound = any as? GFGeoQueryBounds else { return nil }
return db.collection("pickups")
.order(by: "geohash")
.start(at: [bound.startValue])
.end(at: [bound.endValue])
}
var matchingDocs = [QueryDocumentSnapshot]()
// Collect all the query results together into a single list
func getDocumentsCompletion(snapshot: QuerySnapshot?, error: Error?) -> () {
guard let documents = snapshot?.documents else {
print("Unable to fetch snapshot data. \(String(describing: error))")
return
}
print("\nDocs: Count \(documents.count)")
for document in documents {
let lat = document.data()["lat"] as? Double ?? 0
let lng = document.data()["lng"] as? Double ?? 0
let ownerAddress = document.data()["ownerAddress"] as? String ?? "no address"
let coordinates = CLLocation(latitude: lat, longitude: lng)
let centerPoint = CLLocation(latitude: center.latitude, longitude: center.longitude)
// We have to filter out a few false positives due to GeoHash accuracy, but
// most will match
let distance = GFUtils.distance(from: centerPoint, to: coordinates)
print("ownerAddress: \(ownerAddress), distance: \(distance) \tlat: \(lat), \(lng)")
if distance <= radiusInKilometers {
matchingDocs.append(document)
}
}
}
// After all callbacks have executed, matchingDocs contains the result. Note that this
// sample does not demonstrate how to wait on all callbacks to complete.
for query in queries {
query.getDocuments(completion: getDocumentsCompletion)
}
print("Docs: \(matchingDocs.count)")
}
我的距离很远。这是我的输出示例,最上面的是我的查询中心。在 100,000 公里的查询半径下,我仍然没有得到我在加利福尼亚的所有地址。
Home Base: 7401 Sepluveda, Los Angeles CA 90045, distance: 0.0 lat: 33.9742268, -118.3947792
ownerAddress: 1204 Mira Mar Ave, Long Beach CA 90301, distance: 31295.135631869747 lat: 33.781846, -118.1473443
ownerAddress: 625 Fair Oaks, Pasadena CA 91030, distance: 27608.75410615904 lat: 34.1181627, -118.1510438
我的问题是为什么我的距离比 google 地图/现实距离大很多?
firebaser 在这里
从我们在评论中的聊天来看,似乎 withRadius:
值实际上以米为单位,尽管文档是这样说的。
这显然没有按照记录工作,所以我提交了更新文档。 here.
即将发生这种变化
我对 Firebase 中提供的新地理查询功能感到非常兴奋。我正在试用最近推出的 'GeoFire/Utils' pod。我设置了一个测试 iOS 应用程序,这里是 repo。如果有人想克隆并尝试,我会保留规则。我的 Firestore 文档如下所示。
date : January 29, 2021 at 1:29:00 PM UTC-8
geohash:"9mupwu3mkc"
id:"13101C7F-D7FF-4141-BC5A-76602173C096"
lat:33.6863622
lng:-117.8264411
ownerAddress:"1 Civic Center Plaza, Irvine CA 92606"
我正在使用来自 firebase 的示例 code。这是对 firebase
的调用func getallDocs(radius: Double) {
// Find pickups within 50km of Basecamp
let center = CLLocationCoordinate2D(latitude: 33.9742268, longitude: -118.3947792)
let radiusInKilometers: Double = radius
// Each item in 'bounds' represents a startAt/endAt pair. We have to issue
// a separate query for each pair. There can be up to 9 pairs of bounds
// depending on overlap, but in most cases there are 4.
let queryBounds = GFUtils.queryBounds(forLocation: center,
withRadius: radiusInKilometers)
let queries = queryBounds.compactMap { (any) -> Query? in
guard let bound = any as? GFGeoQueryBounds else { return nil }
return db.collection("pickups")
.order(by: "geohash")
.start(at: [bound.startValue])
.end(at: [bound.endValue])
}
var matchingDocs = [QueryDocumentSnapshot]()
// Collect all the query results together into a single list
func getDocumentsCompletion(snapshot: QuerySnapshot?, error: Error?) -> () {
guard let documents = snapshot?.documents else {
print("Unable to fetch snapshot data. \(String(describing: error))")
return
}
print("\nDocs: Count \(documents.count)")
for document in documents {
let lat = document.data()["lat"] as? Double ?? 0
let lng = document.data()["lng"] as? Double ?? 0
let ownerAddress = document.data()["ownerAddress"] as? String ?? "no address"
let coordinates = CLLocation(latitude: lat, longitude: lng)
let centerPoint = CLLocation(latitude: center.latitude, longitude: center.longitude)
// We have to filter out a few false positives due to GeoHash accuracy, but
// most will match
let distance = GFUtils.distance(from: centerPoint, to: coordinates)
print("ownerAddress: \(ownerAddress), distance: \(distance) \tlat: \(lat), \(lng)")
if distance <= radiusInKilometers {
matchingDocs.append(document)
}
}
}
// After all callbacks have executed, matchingDocs contains the result. Note that this
// sample does not demonstrate how to wait on all callbacks to complete.
for query in queries {
query.getDocuments(completion: getDocumentsCompletion)
}
print("Docs: \(matchingDocs.count)")
}
我的距离很远。这是我的输出示例,最上面的是我的查询中心。在 100,000 公里的查询半径下,我仍然没有得到我在加利福尼亚的所有地址。
Home Base: 7401 Sepluveda, Los Angeles CA 90045, distance: 0.0 lat: 33.9742268, -118.3947792
ownerAddress: 1204 Mira Mar Ave, Long Beach CA 90301, distance: 31295.135631869747 lat: 33.781846, -118.1473443
ownerAddress: 625 Fair Oaks, Pasadena CA 91030, distance: 27608.75410615904 lat: 34.1181627, -118.1510438
我的问题是为什么我的距离比 google 地图/现实距离大很多?
firebaser 在这里
从我们在评论中的聊天来看,似乎 withRadius:
值实际上以米为单位,尽管文档是这样说的。
这显然没有按照记录工作,所以我提交了更新文档。 here.
即将发生这种变化