【问题标题】:how to get nearby users(from firebase database) from my location using Firestore?如何使用 Firestore 从我的位置获取附近的用户(来自 firebase 数据库)?
【发布时间】:2020-05-07 05:44:24
【问题描述】:

在我的 Firestore 数据库中,我有用户表,其中包含用户的详细信息及其 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)
            }

       }
    }

【问题讨论】:

  • 您能否分享一下用户地理位置数据在 Firebase 中的样子?
  • pasteboard.co/J7g4LKc.png @zenwraight 请检查这张图片

标签: ios swift iphone xcode google-cloud-firestore


【解决方案1】:

我认为代码可能正常工作,但您的代码中可能存在逻辑错误。

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

希望这会有所帮助!

【讨论】:

  • 谢谢,这正是我所缺乏的,它现在运行良好??
  • 乐于帮助@ap00724 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-05
  • 1970-01-01
  • 1970-01-01
  • 2011-11-19
  • 1970-01-01
  • 2020-12-04
相关资源
最近更新 更多