简体   繁体   English

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

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

I am querying a firestore collection for documents inside of a latitude and longitude range.我正在查询 firestore 集合以查找纬度和经度范围内的文档。 Since it appears you can't conduct a query with arguments given to two different fields ('lat', 'lon'), I have assigned a type Geopoint to a single field 'coords' that has the longitude and latitude.因为看起来你不能用 arguments 给两个不同的字段('lat','lon')进行查询,我已经将类型 Geopoint 分配给具有经度和纬度的单个字段'coords'。 How do I use.where() with geopoints?如何将 .where() 与地理点一起使用? Below is the general query I would like to use, where _latMin, _latMax, etc. are variables linked to the bounding box of my map.下面是我想使用的一般查询,其中 _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());
  });
}

How Do I reference the latitude and longitude inside of coords?如何在坐标中引用经纬度?

在此处输入图像描述

在此处输入图像描述

A Firestore query can only contain range conditions ( > , >= , etc) on one value. Firestore 查询只能包含一个值的范围条件( >>=等)。 So you can filter on either the latitude or longitude of the coords , but not on both in a single query.因此,您可以coords的纬度经度上进行过滤,但不能在单个查询中对两者进行过滤。

If you want to perform geoqueries on Firestore at the moment, the common approach is to store an additional so-called geohash value in your document, which is a (rather magical) string value that combines latitude and longitude in a way that allows you to query them to filter on geographic blocks.如果你现在想在 Firestore 上执行地理查询,常见的方法是在你的文档中存储一个额外的所谓的geohash值,这是一个(相当神奇的)字符串值,它以一种允许你的方式组合纬度和经度查询它们以过滤地理块。

If you want to learn how this works, I recommend checking out this video on the topic: Querying Firebase and Firestore based on geographic location or distance .如果您想了解这是如何工作的,我建议您观看有关以下主题的视频: Querying Firebase and Firestore based on geographical location or distance

If you want to implement this approach on top of Firestore, check out the documentation onimplementing geoqueries on Firestore .如果您想在 Firestore 之上实施此方法,请查看有关在 Firestore 上实施地理查询的文档。

I also recommend checking out these previous questions on the topic:我还建议查看以前关于该主题的问题:

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

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