通过 Firebase 集合进行查询并使用 GeoFlutterFire Flutter 显示附近的用户

问题描述

我在 Firebase 上有两个集合,一个称为 Users,另一个称为 Companies。

我尝试将两个应用程序集成在一起,因此与 Users 集合相关的最终用户应用程序,我希望它通过与 Company 应用程序相关的 Companies 集合进行查询,并使用 GeoFlutterFire 显示最终用户应用程序附近的公司,以某种列表或类似的形式。

我现在拥有的:

我能够将位置数据存储到具有经度和纬度的两个集合中,但我现在不知道如何显示例如附近的公司名称

我有两个功能,不确定哪个最接近正确,所以我会同时发布。

一个函数

Stream showNearbyCompanies() async* {
    var pos = await location.getLocation();

    GeoFirePoint point =
        geo.point(latitude: pos.latitude,longitude: pos.longitude);

    double distance = 30;

    double lat = 0.023323826086956514;
    double lon = 0.02926080000000002776;

    double lowerLat = point.latitude - (lat * distance);
    double lowerLon = point.longitude - (lon * distance);

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

    GeoPoint lesserGeoPoint = GeoPoint(lowerLat,lowerLon);
    GeoPoint greaterGeoPoint = GeoPoint(greaterLat,greaterLon);

    Query query = _firebaseFirestore
        .collection("Companies")
        .where("location",isGreaterThan: lesserGeoPoint)
        .where("location",isLessthan: greaterGeoPoint);

    yield query;
  }

第二个功能

Stream nearbyComp() async* {
    var pos = await location.getLocation();

    GeoFirePoint point =
        geo.point(latitude: pos.latitude,longitude: pos.longitude);
    final CollectionReference users =
        _firebaseFirestore.collection("Companies");

    double radius = 10;
    String field = 'location';

    Stream<List<DocumentSnapshot>> stream = geo
        .collection(collectionRef: users)
        .within(center: point,radius: radius,field: field);

    yield stream;
  }

我想我应该尝试返回一个 StreamBuilder 来检查连接、返回加载,否则返回列表或从当前最终用户附近的集合公司检索的数据。像这样:

Container(
            child: StreamBuilder(
              stream: showNearbyCompanies(),builder: (context,snapshot) {
                if (!snapshot.hasData) {
                  return Text("Loading");
                }
                //return ListView.builder ?
              }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)