使用颤振和Firebase按位置显示附近位置

使用颤振和Firebase按位置显示附近位置,firebase,flutter,google-cloud-firestore,geolocation,geofirestore,Firebase,Flutter,Google Cloud Firestore,Geolocation,Geofirestore,我正在开发一个应用程序使用颤振和Firebase,类似于一个食品递送应用程序。所以,我的问题是,你知道像优步这样的应用程序,你可以选择你的位置,然后应用程序显示你附近的餐馆?这就是我想要达到的目标 我有一个餐馆应用程序,它可以设置位置,我正在firebase上的一个集合中添加这些信息,带有geohash坐标。在终端用户应用程序中,我想在屏幕上显示餐厅名称、图片和菜单等 我所取得的成就: final Stream<QuerySnapshot> _usersStream =

我正在开发一个应用程序使用颤振和Firebase,类似于一个食品递送应用程序。所以,我的问题是,你知道像优步这样的应用程序,你可以选择你的位置,然后应用程序显示你附近的餐馆?这就是我想要达到的目标

我有一个餐馆应用程序,它可以设置位置,我正在firebase上的一个集合中添加这些信息,带有geohash坐标。在终端用户应用程序中,我想在屏幕上显示餐厅名称、图片和菜单等

我所取得的成就:

final Stream<QuerySnapshot> _usersStream =
      FirebaseFirestore.instance.collection('Companies').snapshots();
Container(
            child: StreamBuilder(
              stream: _usersStream,
              builder: (BuildContext context,
                  AsyncSnapshot<QuerySnapshot> snapshot) {
                if (snapshot.hasError) {
                  return Text('Something went wrong');
                }
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Text("Loading");
                }
                print(snapshot.data);
                return new ListView(
                  children: snapshot.data.docs.map((DocumentSnapshot document) {
                    return new ListTile(
                      title: new Text(document.data()['companyName']),
                      subtitle: new Image.network(document.data()['url']),
                    );
                  }).toList(),
                );
              },
            ),
          ),
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);

    Stream<QuerySnapshot> query = _firebaseFirestore
        .collection("Companies")
        .where("location", isGreaterThan: lesserGeoPoint)
        .where("location", isLessThan: greaterGeoPoint)
        .snapshots();

    yield query;
  }
我可以在没有位置部分的情况下实现这一点。因此,目前,每个餐厅都将显示在最终用户应用程序中

这是函数:

final Stream<QuerySnapshot> _usersStream =
      FirebaseFirestore.instance.collection('Companies').snapshots();
Container(
            child: StreamBuilder(
              stream: _usersStream,
              builder: (BuildContext context,
                  AsyncSnapshot<QuerySnapshot> snapshot) {
                if (snapshot.hasError) {
                  return Text('Something went wrong');
                }
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Text("Loading");
                }
                print(snapshot.data);
                return new ListView(
                  children: snapshot.data.docs.map((DocumentSnapshot document) {
                    return new ListTile(
                      title: new Text(document.data()['companyName']),
                      subtitle: new Image.network(document.data()['url']),
                    );
                  }).toList(),
                );
              },
            ),
          ),
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);

    Stream<QuerySnapshot> query = _firebaseFirestore
        .collection("Companies")
        .where("location", isGreaterThan: lesserGeoPoint)
        .where("location", isLessThan: greaterGeoPoint)
        .snapshots();

    yield query;
  }
最终流\u用户流=
FirebaseFirestore.instance.collection('companys').snapshots();
这是我用来显示收藏的列表视图:

final Stream<QuerySnapshot> _usersStream =
      FirebaseFirestore.instance.collection('Companies').snapshots();
Container(
            child: StreamBuilder(
              stream: _usersStream,
              builder: (BuildContext context,
                  AsyncSnapshot<QuerySnapshot> snapshot) {
                if (snapshot.hasError) {
                  return Text('Something went wrong');
                }
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Text("Loading");
                }
                print(snapshot.data);
                return new ListView(
                  children: snapshot.data.docs.map((DocumentSnapshot document) {
                    return new ListTile(
                      title: new Text(document.data()['companyName']),
                      subtitle: new Image.network(document.data()['url']),
                    );
                  }).toList(),
                );
              },
            ),
          ),
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);

    Stream<QuerySnapshot> query = _firebaseFirestore
        .collection("Companies")
        .where("location", isGreaterThan: lesserGeoPoint)
        .where("location", isLessThan: greaterGeoPoint)
        .snapshots();

    yield query;
  }
容器(
孩子:StreamBuilder(
流:_usersStream,
生成器:(BuildContext上下文,
异步快照(快照){
if(snapshot.hasError){
返回文本(“出错”);
}
if(snapshot.connectionState==connectionState.waiting){
返回文本(“加载”);
}
打印(快照数据);
返回新的ListView(
子项:snapshot.data.docs.map((DocumentSnapshot文档){
返回新的ListTile(
标题:新文本(document.data()['companyName']),
字幕:新建Image.network(document.data()['url']),
);
}).toList(),
);
},
),
),
有没有人知道我需要更改什么,以便我可以使用设置的半径进行查询?我知道有一些方法,比如“.where('field',isLowerThan:x)”,但我无法让它工作

另外,这里是我的firebase的屏幕截图:

final Stream<QuerySnapshot> _usersStream =
      FirebaseFirestore.instance.collection('Companies').snapshots();
Container(
            child: StreamBuilder(
              stream: _usersStream,
              builder: (BuildContext context,
                  AsyncSnapshot<QuerySnapshot> snapshot) {
                if (snapshot.hasError) {
                  return Text('Something went wrong');
                }
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Text("Loading");
                }
                print(snapshot.data);
                return new ListView(
                  children: snapshot.data.docs.map((DocumentSnapshot document) {
                    return new ListTile(
                      title: new Text(document.data()['companyName']),
                      subtitle: new Image.network(document.data()['url']),
                    );
                  }).toList(),
                );
              },
            ),
          ),
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);

    Stream<QuerySnapshot> query = _firebaseFirestore
        .collection("Companies")
        .where("location", isGreaterThan: lesserGeoPoint)
        .where("location", isLessThan: greaterGeoPoint)
        .snapshots();

    yield query;
  }

编辑:

final Stream<QuerySnapshot> _usersStream =
      FirebaseFirestore.instance.collection('Companies').snapshots();
Container(
            child: StreamBuilder(
              stream: _usersStream,
              builder: (BuildContext context,
                  AsyncSnapshot<QuerySnapshot> snapshot) {
                if (snapshot.hasError) {
                  return Text('Something went wrong');
                }
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Text("Loading");
                }
                print(snapshot.data);
                return new ListView(
                  children: snapshot.data.docs.map((DocumentSnapshot document) {
                    return new ListTile(
                      title: new Text(document.data()['companyName']),
                      subtitle: new Image.network(document.data()['url']),
                    );
                  }).toList(),
                );
              },
            ),
          ),
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);

    Stream<QuerySnapshot> query = _firebaseFirestore
        .collection("Companies")
        .where("location", isGreaterThan: lesserGeoPoint)
        .where("location", isLessThan: greaterGeoPoint)
        .snapshots();

    yield query;
  }
这是餐厅应用程序上的代码,我正在使用Google Places API存储地质点:

Future<Null> displayPrediction(Prediction p) async {
    if (p != null) {
      PlacesDetailsResponse detail =
          await _places.getDetailsByPlaceId(p.placeId);

      double lat = detail.result.geometry.location.lat;
      double lng = detail.result.geometry.location.lng;

      GeoPoint point = new GeoPoint(lat, lng);

      final CollectionReference companies =
          _firebaseFirestore.collection("Companies");
      final String uid = _firebaseAuth.currentUser.uid;
      await companies.doc(uid).update({'location': point});
      final result = await companies.doc(uid).get();
      return result.data()["location"];
    }
  }
未来显示预测(预测p)异步{
如果(p!=null){
位置细节响应细节=
等待地点。getDetailsByPlaceId(p.placeId);
双lat=detail.result.geometry.location.lat;
双液化天然气=详图.result.geometry.location.lng;
地质点=新的地质点(纬度、液化天然气);
最终收集参考公司=
_firebaseFirestore.集合(“公司”);
最终字符串uid=\u firebaseAuth.currentUser.uid;
wait companys.doc(uid).update({'location':point});
最终结果=wait companys.doc(uid.get();
返回result.data()[“location”];
}
}
我试图使用经度和纬度的代码片段:

final Stream<QuerySnapshot> _usersStream =
      FirebaseFirestore.instance.collection('Companies').snapshots();
Container(
            child: StreamBuilder(
              stream: _usersStream,
              builder: (BuildContext context,
                  AsyncSnapshot<QuerySnapshot> snapshot) {
                if (snapshot.hasError) {
                  return Text('Something went wrong');
                }
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Text("Loading");
                }
                print(snapshot.data);
                return new ListView(
                  children: snapshot.data.docs.map((DocumentSnapshot document) {
                    return new ListTile(
                      title: new Text(document.data()['companyName']),
                      subtitle: new Image.network(document.data()['url']),
                    );
                  }).toList(),
                );
              },
            ),
          ),
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);

    Stream<QuerySnapshot> query = _firebaseFirestore
        .collection("Companies")
        .where("location", isGreaterThan: lesserGeoPoint)
        .where("location", isLessThan: greaterGeoPoint)
        .snapshots();

    yield query;
  }
Stream shownnearbycompanies()异步*{
var pos=await location.getLocation();
地火点=
地理点(纬度:位置纬度,经度:位置经度);
双倍距离=30;
双板条=0.023323826086956514;
双lon=0.0292608000002776;
双下纬度=点纬度-(纬度*距离);
双下经度=点经度-(经度*距离);
双大纬度=点纬度+(纬度*距离);
double greaterLon=点经度+(经度*距离);
地质点lesserGeoPoint=地质点(lowerLat,lowerLon);
地质点greaterGeoPoint=地质点(greaterLat,greaterLon);
流查询=\u firebaseFirestore
.收款(“公司”)
.where(“位置”,大于:lesserGeoPoint)
.其中(“位置”,小于:greaterGeoPoint)
.快照();
产量查询;
}

我在屏幕截图中没有看到任何geohash,也没有在代码中看到任何对geohash的过滤。你确定你遵循了上的文档吗?在你的问题中,你说“使用geohash坐标”,但就我所见,屏幕截图中没有geohash,代码中也没有。我建议学习我链接的文档,因为它以一种比我们在这里做得更好的方式概括了这些步骤。嗨!我会使用地理哈希,因为使用纬度和经度不允许您查询@FrankvanPuffelen共享的文档中解释的结果。然后,要查询GeoHash,必须选择当前设备位置和半径并跟随。另外,请您提供一个片段,说明您是如何通过radius进行查询的?您的问题已标记为GeoFirestore-所以。。。你为什么不用它?这使得这项任务变得微不足道。还有,你已经问过这个问题了。你为什么一遍又一遍地问同一个问题?请不要一遍又一遍地发布同一个问题。它最终可能会让你被禁止。发布一个问题,当评论进来并要求更多信息时,不断完善它。例如,我在上面问了一个特定的问题,而您没有解决它或添加更多的细节。这又是一个问题:您已将问题标记为,但没有代码指示您正在使用它。GeoFirestore使此任务变得微不足道,并且完全按照您的要求执行。一行代码返回给定半径内的所有餐厅。你为什么不用它?