【问题标题】:How to compare multiple fields in a query from Firestore?如何比较 Firestore 查询中的多个字段?
【发布时间】:2021-09-01 10:14:50
【问题描述】:

我正在尝试找到一种方法来比较所有用户的位置并显示附近的人。 In this question,弗兰克解释了应该如何做。但是我不知道怎么做第三步。

这就是我目前所取得的成就:

double _userLatitude;
double _userLongitude;

_getUserLocation() async {
    Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);

    _userLatitude = position.latitude;
    _userLongitude = position.longitude;

    final firebaseUser = await FirebaseAuth.instance.currentUser();
    if (firebaseUser != null)
      await usersRef.document(firebaseUser.uid).updateData({
        "location": "active",
        "latitude": _userLatitude,
        "longitude": _userLongitude,
      });
  }

_getNearbyUsers() {
    usersRef.where('location', isEqualTo: "active").getDocuments();

    Geolocator.distanceBetween(
        _userLatitude, _userLongitude, endLatitude, endLongitude);
  }

StreamBuilder(
        stream: _getNearbyUsers(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Center(
              child: Text(
                "Loading...",
              ),
            );
          } else if (snapshot.data.documents.length == 1) {
            return Center(
              child: Text(
                ":/",
              ),
            );
          }
          return ListView.builder(
              padding: EdgeInsets.only(top: 10),
              scrollDirection: Axis.vertical,
              itemCount: snapshot.data.documents.length,
              itemBuilder: (context, index) =>
                  _buildListUsers(context, snapshot.data.documents[index]));
        },
      ),

 Widget _buildListUsers(BuildContext context, DocumentSnapshot document) {
    List<Users> usersList = [];
    var data = document.data;
    if (data["id"] != _userId) {
      Users user = Users();
      user.id = document["id"];

      usersList.add(user);
      return ListTile(
        onTap: () {
          Navigator.pushNamed(context, "/users", arguments: user);
        },
        title: Text(
                  document['id'],
              ),
      );
    }
    return SizedBox();
  }

我不知道接下来要在 _getNearbyUsers() 中做什么。

【问题讨论】:

    标签: firebase flutter


    【解决方案1】:

    Geolocator.distanceBetween 函数以米为单位返回距离。对于距离,您应该为您的应用程序应用逻辑,例如,如果距离小于 500m,则执行操作。

    类似这样的:

    double distanceInMeters = Geolocator.distanceBetween(_userLatitude, _userLongitude, endLatitude, endLongitude);
    if (distanceInMeters < 500) {
    // Your custom logic goes here
    }
    

    【讨论】:

    • 谢谢,这会有所帮助,但我最大的问题是如何获取所有其他用户的位置并与当前用户进行比较。关于如何做到这一点的任何想法?
    • 我相信您应该定期将用户位置保存在数据库中,每分钟一次,甚至更多,具体取决于您的需要。然后对该数据库运行查询。我已经使用 mysql 完成了这项工作,但从未使用 firebase realtime 或 firestore。 Firestore 似乎有地理查询 firebase.google.com/docs/firestore/solutions/geoqueries
    猜你喜欢
    • 2019-08-14
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 2017-02-12
    • 1970-01-01
    • 2021-08-14
    相关资源
    最近更新 更多