0

I'm trying to find a way to compare the location of all users and show nearby people as a result. In this question, Frank explains how it should be done. But I have no idea how to do the third step.

That's what I've achieved so far:

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();
  }

I have no idea what to do next in _getNearbyUsers().

Vitor Freitas
  • 103
  • 1
  • 11

1 Answers1

0

The Geolocator.distanceBetween function returns the distance in meters. With the distance you should apply the logic for your application, like performing an action if the distance is smaller than 500m, for example.

Something like this:

double distanceInMeters = Geolocator.distanceBetween(_userLatitude, _userLongitude, endLatitude, endLongitude);
if (distanceInMeters < 500) {
// Your custom logic goes here
}
  • Thanks, this will help but my biggest problem is how to get the location of all other users and compare with the current user. Any idea on how to do this? – Vitor Freitas Jun 16 '21 at 15:52
  • 1
    I believe you should save the users location periodically in a database, once a minute or maybe even more depending on your needs. And then run a query against this database. I've done this already using mysql, but never using firebase realtime or firestore. Firestore seems to have geoqueries https://firebase.google.com/docs/firestore/solutions/geoqueries – Leonardo Ferreira Jun 16 '21 at 17:47