Flutter Firebase GeoQueries 返回大量虚假数据

bad*_*asW 5 search geohashing firebase flutter google-cloud-firestore

我正在构建一个像 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');
     
    });
  });
}
Run Code Online (Sandbox Code Playgroud)

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

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

Fra*_*len 3

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

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

在此输入图像描述

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

如果您有兴趣了解更多信息,我建议您观看我几年前所做的演讲视频:根据地理位置或距离查询 Firebase 和 Firestore