【问题标题】:Flutter - How get documents from Cloud Firestore ordering by geo distance?Flutter - 如何通过地理距离从 Cloud Firestore 中获取文档?
【发布时间】:2021-07-06 03:27:21
【问题描述】:

我有一个类似于 Tinder 的约会应用程序,但对于特定的利基市场,该应用程序将用户的纬度和经度存储在 Cloud Firestore 数据库上的文档中,我有一个方法可以返回与用户的距离(以米为单位),我是为此使用 GeoLocator,我知道当前用户和另一个用户之间的距离。

static String getDistanceBetween({
    double sourceLat, 
    double sourceLong,
    double currentLat,
    double currentLong
      }) {

    double _distanceInMeters = Geolocator.distanceBetween(
      sourceLat,
      sourceLong,
      currentLat,
      currentLong,
    );

    print(_distanceInMeters);

    if(_distanceInMeters == null){
      return "?";
    } else if(_distanceInMeters == 0.0){
      return "0";
    } else {
     return _distanceInMeters.toString().substring(0, 5);
    }


  }

当用户登录时,我想先检索最近的用户,从当前登录的用户中检索最近的用户的文档最简单的方法是什么?

如果我能做这样的事情,我认为它可以解决问题,但我认为 Firestore 不可能:

FirebaseFirestore.instance.collection("users").orderBy(getDistanceBetween(), descending: true).get().then((querySnapshot){

...
/// Get documents from users that are nearest to the current user

})

【问题讨论】:

    标签: flutter google-cloud-firestore geolocation


    【解决方案1】:

    没有办法按距离排序,因为这需要数据库对每个文档执行计算,这将无法满足其性能保证。

    您可以做的是从第二个用户那里检索一定范围内的文档。这在 geoqueries 上的 Firestore 文档中有所描述,但您也可以阅读其中一些以前的 questions on the topic,其中许多使用附加库来完成相同的操作。

    在检索到范围内的文档后,您需要在应用程序代码中按它们的距离对它们进行排序。

    【讨论】:

      猜你喜欢
      • 2021-11-19
      • 1970-01-01
      • 2021-05-27
      • 2020-12-05
      • 2021-01-28
      • 1970-01-01
      • 2023-03-28
      • 2020-07-02
      • 2019-05-05
      相关资源
      最近更新 更多