繁体   English   中英

Firebase 和 Swift 中的 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 中的示例代码。这是对 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)")
}

我的距离很远。 这里以我的output为例,最上面的是我的查询中心。 即使查询半径为 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

我的问题是为什么我的距离比谷歌地图/现实距离大得多?

炮兵在这里

从我们在评论中的聊天来看, withRadius:值实际上以米为单位,尽管文档中有说明。

这显然没有按照记录工作,所以我提交了一份文件来更新文档。 这种变化正在发生

我对 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 的示例代码 这是对 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)")
}

我的距离很遥远。 下面是我的output的例子,最上面的是我的查询中心。 查询半径为 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

我的问题是为什么我的距离比谷歌地图/现实大得多?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM