繁体   English   中英

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

[英]How do I Query Firebase Firestore for a field of type Geopoint in Flutter?

我正在查询 firestore 集合以查找纬度和经度范围内的文档。 因为看起来你不能用 arguments 给两个不同的字段('lat','lon')进行查询,我已经将类型 Geopoint 分配给具有经度和纬度的单个字段'coords'。 如何将 .where() 与地理点一起使用? 下面是我想使用的一般查询,其中 _latMin、_latMax 等是链接到我的 map 边界框的变量。

_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());
  });
}

如何在坐标中引用经纬度?

在此处输入图像描述

在此处输入图像描述

Firestore 查询只能包含一个值的范围条件( >>=等)。 因此,您可以coords的纬度经度上进行过滤,但不能在单个查询中对两者进行过滤。

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

如果您想了解这是如何工作的,我建议您观看有关以下主题的视频: Querying Firebase and Firestore based on geographical location or distance

如果您想在 Firestore 之上实施此方法,请查看有关在 Firestore 上实施地理查询的文档。

我还建议查看以前关于该主题的问题:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM