ios - 使用 Swift 在 Firebase 中进行 GeoHash 查询

标签 ios swift firebase google-cloud-firestore geolocation

我对 Firebase 中提供的新地理查询功能感到非常兴奋。我正在尝试最近推出的“GeoFire/Utils” pods 。我设置了一个测试 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"

我正在使用示例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

从我们在评论中的聊天看来,withRadius: 值实际上以米为单位,无论文档如何规定。

这显然不符合文档要求,因此我提交了一份文件来更新文档。这种变化将在 here 中出现。 .

关于ios - 使用 Swift 在 Firebase 中进行 GeoHash 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66106984/

相关文章:

iOS sqlite blob 写入失败,出现 'unknown error'

ios - 编写扩展文件属性 swift 示例

ios - swift - 如何在 NSTimer 中使用 FIRE()

firebase - 如何非规范化/规范化 Firebase 实时数据库的数据结构?

firebase - "Catch all other"Firebase 数据库规则

ios - 使用 Core Image iOs 进行人脸检测

ios - 正确理解 swift 中的prepareforsegue

ios - (Swift 2.1) 在 MKMapView 中加载可见区域的注释

android - Firebase Analytics 不在仪表板中显示任何数据

c# - xamarin ios c# 下划线 UIButton 文本