代码之家  ›  专栏  ›  技术社区  ›  greenzebra

如何在Flutter中查询Firebase Firestore中的Geopoint类型字段?

  •  0
  • greenzebra  · 技术社区  · 4 年前

    我在firestore集合中查询纬度和经度范围内的文档。由于您似乎无法使用给定给两个不同字段(“lat”、“lon”)的参数进行查询,因此我已将类型Geopoint分配给具有经度和纬度的单个字段“coords”。如何将.where()用于地理点?下面是我想使用的常规查询,其中_latMin、_latMax等是链接到地图边界框的变量。

    _getDocs() async {
      print('getdocs');
      final QuerySnapshot snapshot = await FirebaseFirestore.instance
          .collection('collectionName')
          .where(
            'coords',
            isGreaterThan: _latMin,
            isLessThan: _latMax,
          )
          .where(
            'coords',
            isGreaterThan: _lonMin,
            isLessThan: _lonMax,
          )
          .get();
      snapshot.docs.forEach((DocumentSnapshot doc) {
        print(doc.data());
      });
    }
    

    如何在坐标系中参考纬度和经度?

    enter image description here

    enter image description here

    2 回复  |  直到 4 年前
        1
  •  8
  •   Frank van Puffelen    4 年前

    Firestore查询只能包含范围条件( > , >= 等)对一个值。所以你 可以 过滤器打开 任何一个 coords ,但不能在单个查询中同时对这两个查询执行。

    如果您现在想在Firestore上执行地理查询,常见的方法是存储一个额外的所谓 geohash 值,这是一个(相当神奇的)字符串值,它结合了纬度和经度,允许您查询它们以过滤地理块。

    如果你想了解这是如何工作的,我建议你看看这个主题的视频: Querying Firebase and Firestore based on geographic location or distance

    如果您想在Firestore之上实现这种方法,请查看 implementing geoqueries on Firestore

    我还建议你看看之前关于这个主题的这些问题: