【发布时间】:2021-05-12 09:00:26
【问题描述】:
我对 Firebase 中提供的新地理查询功能感到非常兴奋。我正在试用最近推出的“GeoFire/Utils”吊舱。我设置了一个测试 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
我的问题是为什么我的距离比谷歌地图/现实的距离要大得多?
【问题讨论】:
-
firebaser here 我想知道该值是否实际上不是以米为单位,尽管我们的文档说了什么。当您将半径解释为以米为单位时,您可以尝试看看结果是否更有意义?
-
嗨@FrankvanPuffelen,你和firebase都摇滚!我喜欢你的视频!伟大的想法。从基地到长滩接送的距离为 24.4 英里或 39.2 公里(通过高速公路英里)。我的地理查询结果是 31,295.. 如果以米为单位,则为 31.2 公里——因为鸟儿飞过。所以这在目标上要多一些。加利福尼亚州的长度为 1,240 公里,因此如果我想包括整个州的 120 万米。我尝试将其作为双精度快速传递,但编译器说该值太高了双人大
-
是的,我相信就是这样@FrankvanPuffelen。当我经过 1,200,000 米时,我会在整个加利福尼亚州获得接送服务。我今天没有收到 bizarro swift 错误。所以距离的输出以米为单位,我必须考虑距离不是我从谷歌地图获得的高速公路英里。非常感谢您的帮助!
-
唷,这就解释了。抱歉,您遇到了这个问题,但我很高兴我们似乎找到了它。我们在 Android 和 JavaScript 库中也有这方面的错误,所以我将检查我们需要更改的内容(文档和/或代码)。
标签: ios swift firebase google-cloud-firestore geolocation