firebase - Flutter Firebase GeoQueries 正在返回大量虚假数据
问题描述
我正在构建一个像 Uber Eats 一样的订餐应用程序。所以,我正在使用颤振制作一个应用程序,但是现在我遇到了寻找最近餐馆的问题。
我目前正在使用 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');
});
});
}
是的,我从数据库返回最近的地方,但是我得到了 30 多个结果,其中只有 6-7 个是正确的,我认为扩展效率不高,因为它增加了我的读取和开销在客户端过滤它们。
现在我的问题是,如何实现一个完美的地理查询来过滤 Cloud Firestore 中最近的餐馆和司机?请帮忙。
解决方案
Firestore 的大多数地理查询实现(如Firebase 本身记录的那个)使用geohash值来允许对两个值(纬度/经度)进行范围查询。虽然 geohash 值允许地理查询运行,但根据定义,它们是不精确的,并且它们匹配矩形区域中的项目。
当您在绿色区域查找文档时,geohashes 的范围实际上可能会扩展到包括红色区域:
要将返回的项目限制为您想要的圆形半径,您需要在执行过程中对结果进行后处理。在我的测试中,查询读取的文档比需要的多 5 倍,一些查询(通常是结果更少的查询)读取的文档多达 15 倍。基于此,您的结果似乎是平均水平。
如果您有兴趣了解更多信息,我建议您观看我几年前的演讲视频:Querying Firebase and Firestore based on geolocation or distance。
推荐阅读
- azure-synapse - 启用 Azure Synapse DEP 的工作区出错
- sql-server - 从 Angular 到 Net Core HTTP 404 发布错误
- javascript - React ReCharts Y 轴刻度不等间距
- vba - 将变量从一种形式传递到另一种形式
- mysql - 可以在 mysql 中将小视频(少于 5 秒)存储为 blob 吗?
- flutter - 如何降低 DropdownMenuItem 的高度?
- android-studio - 在 Android Studio 北极狐 M1 中通过 Force Touch 进行“前往声明”
- symfony - Symfony 中带有 esi 的 Cloudflare 缓存
- python - 按 pyspark 日期过滤文件
- python - 有没有办法在一行上打印用户定义的对象列表(每个对象都是一个多行字符串)?