Flutter Firestore地理查询 [英] Flutter Firestore Geo query

查看:50
本文介绍了Flutter Firestore地理查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该怎么做:

我正在构建一个应用程序,我想在firebase中进行查询并接收X公里范围内的用户. 但是我想限制接收到的文档,并且要具有在滚动条上获取更多文档的功能.我正在使用StreamBuilder->ListView.build来建立我的列表.当我要接收可滚动列表的末尾时,我想从Firestore获取更多文档(如果有的话).

I'm building an application where I want to do a query in firebase and receive the users who are in a range of X kilometers. But I want to limit this received documents and also to have a function for getting more documents on the scroll. I'm using a StreamBuilder->ListView.build in order to build my list. When I'm going to receive the end of the scrollable list I want to get more documents from Firestore (if I have any).

我已经完成的事情:

到目前为止,我仅找到了2个解决问题的方法:

So far I found just 2 solutions for my problem:

  1. Firestore助手
  2. GeoFlutterFire
  1. Firestore Helpers
  2. GeoFlutterFire


问题:

这两个库都很好,只存在一个 BIG 问题,在这两种情况下,我都无法限制要接收的文档数量(我可以将原始查询限制为Firestore.instance.collection("MyColection")),但不会对我有太大帮助,因为我将遇到这种情况

Both of those libraries are good with just one BIG problem, in both of this case I'm not able to limit the number of documents that I'm going to receive (I can JUST limit the original query for Firestore.instance.collection("MyColection")) but is not going to help me that much because I will have this case

从Firestore给我10个文档->给我5公里范围的用户-> 假设我只会收到4个.

Give me 10 documents from Firestore -> Give me the 5km range users -> I will receive just 4 let's say.

我想要的是从Firestore接收X范围内的10个文档.

What I want instead is to receive 10 documents from Firestore within a range of X.

这个问题有解决方案吗?还是我需要为此实现自己的库?还是不可能呢?

Is there any solution for this problem? Or I need to implement my own library for this? Or is not possible even so?

推荐答案

GeoPoint userGeoPoint = user.geoPoint ["geopoint"];

GeoPoint userGeoPoint = user.geoPoint["geopoint"];

//距离(以英里为单位) 双倍距离= 30;

// distance in miles double distance = 30;

double lat = 0.0144927536231884;
double lon = 0.0181818181818182;
double lowerLat = userGeoPoint.latitude - (lat * distance);
double lowerLon = userGeoPoint.longitude - (lon * distance);

double greaterLat = userGeoPint.latitude + (lat * distance);
double greaterLon = userGeoPint.longitude + (lon * distance);

GeoPoint lesserGeopoint = GeoPoint(lowerLat, lowerLon);
GeoPoint greaterGeopoint = GeoPoint(greaterLat, greaterLon);
Query query = Firestore.instance
    .collection(path)
    .where("geoPoint.geopoint", isGreaterThan: lesserGeopoint)
    .where("geoPoint.geopoint", isLessThan: greaterGeopoint)
    .limit(limit);

这对我有用.希望对您有所帮助.

这篇关于Flutter Firestore地理查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆