如何在 Firebase Cloud Firestore 的集合中查询最近的 GeoPoints?

How to query closest GeoPoints in a collection in Firebase Cloud Firestore?

新的 Firestore DB 允许我存储 GeoPoints。有没有办法根据它们进行查询?

例如,如果我的每个集合文档都有一个 location 类型的 geopoint 字段。 如何获取距离任意坐标最近的10个文档?

文档似乎没有涵盖这一点。我试过这样的东西:

someRef.where('location', '>', geopoint).limit(10).get();

显然这没有多大意义,但我只是在这里尝试一些东西

我们还没有公开地理查询,所以目前没有办法做到这一点。

历史上,Firebase 实时数据库使用 geohashes in a library called GeoFire--but given that we plan to expose actual geoqueries in the near future, we haven't updated that library to Firestore. To learn how to do something similar yourself though, have a look at this video

您可以让 Algolia 为您进行带半径的地理搜索。它所需要的只是构建一个云函数来触发您想要的文档并将它们同步到 Algolia。

看看地理搜索https://www.algolia.com/doc/guides/searching/geo-search/

此外,Firebase 文档还在此处宣传 Algolia 作为复杂搜索的解决方案 https://firebase.google.com/docs/firestore/solutions/search

自@problemsofsumit 首次提出此问题以来,已引入一个新项目。该项目称为 GEOFirestore。

有了这个库,你可以在一个圆圈内执行类似查询文档的查询:

  const geoQuery = geoFirestore.query({
    center: new firebase.firestore.GeoPoint(10.38, 2.41),
    radius: 10.5
  });

您可以通过 npm 安装 GeoFirestore。您必须单独安装 Firebase(因为它是 GeoFirestore 的对等依赖项):

$ npm install geofirestore firebase --save

您可以通过 cdn link 访问它: https://cdn.jsdelivr.net/npm/geofirestore@2.2.2/dist/geofirestore.min.js

我不会使用像 Algolia 这样的第三方服务,而是使用为 iOS 和 Android 推出的新库,它为 Firestore 复制了 GeoFire。名为 GeoFirestore 的新库已完整记录并经过充分测试。我已经在许多演示项目中使用过这个库,它似乎工作得很好。试一试!

在搜索网络后,我发现了 chintan369 的这个 GeoFirestore 库,您可以使用它来查询附近的地理点。这些查询基于 Firestore 本机查询。只需将以下行添加到您的 build.gradle(项目级别):

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

然后将库添加到您的 build.gradle(应用程序级别):

dependencies {
  implementation 'com.github.chintan369:Geo-FireStore-Query:1.1.0'
}

...并同步您的项目。

访问 https://medium.com/android-kotlin/geo-firestore-query-with-native-query-bfb0ac1adf0a 了解更多。