【问题标题】:Flutter Firebase GeoQueries is giving back a lot of false dataFlutter Firebase GeoQueries 正在返回大量虚假数据
【发布时间】:2021-11-04 12:23:34
【问题描述】:

我正在构建一个像 Uber Eats 一样的订餐应用。所以,我正在使用 Flutter 制作一个应用程序,但是现在我遇到了寻找最近餐厅的问题。

我目前正在使用 geoflutterfire 包存储位置详细信息。请看附图。

看看我的代码。

getLocation() async {
// Create a geoFirePoint

  GeoFirePoint center =
      geo.point(latitude: 28.706707247639613, longitude: 80.58572410373661);

// get the collection reference or query
  var collectionReference = _firestore.collection('locations');

  double radius = 5;
  String field = 'position';

  Stream<List<DocumentSnapshot>> stream = geo
      .collection(collectionRef: collectionReference)
      .within(center: center, radius: radius, field: field);

  stream.forEach((element) {
    element.forEach((e) {
      double fbLat = e.get('position')['geopoint'].latitude;
      double fbLng = e.get('position')['geopoint'].longitude;

      LatLng loc2 = LatLng(fbLat, fbLng);
      LatLng loc1 = LatLng(center.latitude, center.longitude);

      final distance =
          distanceBetweenTwoLocations(location1: loc1, location2: loc2);
      print('=======distance is ======');
      print('${distance / 1000}KM');
     
    });
  });
}

是的,我从数据库中返回最近的位置,但是我返回了 30 多个结果,其中只有 6-7 个是正确的,我认为扩展效率不高,因为它增加了我的读取以及在客户端过滤它们的开销。

现在我的问题是,如何实施完美的地理查询来过滤 Cloud Firestore 中最近的餐馆和司机?请帮忙。

【问题讨论】:

    标签: firebase flutter search google-cloud-firestore geohashing


    【解决方案1】:

    Firestore 的大多数地理查询实现(例如 documented by Firebase itself)使用 geohash 值来允许对两个值(纬度/经度)进行范围查询。虽然 geohash 值允许地理查询发挥作用,但根据定义它们并不精确,并且它们匹配矩形区域中的项目。

    当您在绿色区域查找文档时,geohashes 的范围实际上可能会扩展到包括红色区域:

    要将返回的项目限制在您想要的圆形半径内,您需要在执行过程中对结果进行后处理。在我的测试中,查询读取的文档比需要的多 5 倍,一些查询(通常是结果更少的查询)读取的文档多达 15 倍。基于此,您的结果似乎是平均水平。

    如果您有兴趣了解更多信息,我建议您观看我几年前的演讲视频:Querying Firebase and Firestore based on geographic location or distance

    【讨论】:

    • 非常感谢您的详细解释。我同意你的观点,并且也观看了整个视频。还有一个问题,有没有办法限制数据,以便我可以实现分页。
    • 虽然您当然可以为查询添加限制,但这取决于您使用的库。文档的链接显示了可以轻松添加此类条件的地方。但它可能不会按照您的意愿进行操作,因为结果不会按距离排序,并且您将分页文档,稍后可能会剔除(红色区域中的文档)。
    猜你喜欢
    • 1970-01-01
    • 2021-04-21
    • 2021-06-07
    • 1970-01-01
    • 2021-06-02
    • 2016-12-05
    • 2021-09-28
    • 2019-08-05
    • 1970-01-01
    相关资源
    最近更新 更多