如何使用 Firestore 从我的位置获取附近的用户(来自 firebase 数据库)?

how to get nearby users(from firebase database) from my location using Firestore?

在我的 Firestore 数据库中,我有用户 table 具有详细信息及其 GeoPoint 的用户。

我想通过检查数据库中的用户是否在我的范围内来获取 GMSCoordinateBounds 中我所在位置附近的用户。

对于该问题的任何更多查询评论。

目前我正在做的是但没有得到正确的结果:-

    let visibleRegionn = mapView.projection.visibleRegion()
    let bounds = GMSCoordinateBounds(region: visibleRegionn)
    // we've got what we want, but here are NE and SW points
    let northEast = bounds.northEast
    let southWest = bounds.southWest

    let geopoint1 = GeoPoint(latitude: northEast.latitude, longitude: northEast.longitude)
    let geopoint2 = GeoPoint(latitude: southWest.latitude, longitude: southWest.longitude)


    let docRef = Firestore.firestore().collection("Users")
    let query =
        docRef
        .whereField("locationGeopoint", isLessThanOrEqualTo: geopoint1)
        .whereField("locationGeopoint", isLessThanOrEqualTo: geopoint2)

    query.getDocuments { snapshot, error in
        if let error = error {
            print("Error getting documents: \(error)")
        } else {
            for document in snapshot!.documents {
                self.arrListMapData.append(document.data() as NSDictionary)
            }

       }
    }

我认为代码可能有效,但您的代码中可能存在逻辑错误。

let visibleRegionn = mapView.projection.visibleRegion()
let bounds = GMSCoordinateBounds(region: visibleRegionn)
// we've got what we want, but here are NE and SW points
let northEast = bounds.northEast
let southWest = bounds.southWest

let geopoint1 = GeoPoint(latitude: northEast.latitude, longitude: northEast.longitude)
let geopoint2 = GeoPoint(latitude: southWest.latitude, longitude: southWest.longitude)


let docRef = Firestore.firestore().collection("Users")
let query =
    docRef
    .whereField("locationGeopoint", isLessThan: geopoint1)
    .whereField("locationGeopoint", isGreaterThan: geopoint2)

query.getDocuments { snapshot, error in
    if let error = error {
        print("Error getting documents: \(error)")
    } else {
        for document in snapshot!.documents {
            self.arrListMapData.append(document.data() as NSDictionary)
        }

   }
}

如果你看到你在两个地方都有 - isLessThanOrEqualTo,这实际上不会给你正确的结果。您需要一个范围,所以现在您想要找到该范围内的所有用户,因此您需要使用类似 - isLessThanisGreaterThan.

希望对您有所帮助!