Search code examples
javaandroidfirebasefirebase-realtime-databasegeofire

Retrieve a list of users based on distance with geofire & firebase


Hey all I've seen a number of questions on this but none that seem to answer my particular question. I'm trying to retrieve a list of users based on their distance. My Firebase database structure looks like this:

enter image description here

Within the MainActivity I'd running the following methods to gain a result:

private List<User> generateProfiles(){
        uidList = getUserList();
        Log.d("UidList",String.valueOf(uidList.size()));
           DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
           DatabaseReference phoneNumberRef = rootRef.child("users");
           phoneNumberRef.addValueEventListener(new ValueEventListener() {
               @Override
               public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                   for (DataSnapshot ds : dataSnapshot.getChildren()) {
                       try{
                            User tempUser = ds.getValue(User.class);
                            userList.add(tempUser);
                         }
                       catch (IndexOutOfBoundsException e) {
                           Log.d("Index EXception", e.getMessage());
                       }
                   }
                   Log.d("PRIORUSER", userList.toString());
               }

               @Override
               public void onCancelled(DatabaseError databaseError) {
               }
           });


       return userList;
    }

Which calls this method to get a list of users within the geofire query distance:

private List<String> getUserList(){
        try{
        mFusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                uidList.clear();
                double lattitude = location.getLatitude();
                double longitude = location.getLongitude();
                DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("GeoFire");
                GeoFire fire = new GeoFire(ref);
                GeoQuery geoQuery = fire.queryAtLocation(new GeoLocation(lattitude,longitude),8);
                geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
                    @Override
                    public void onKeyEntered(String key, GeoLocation location) {
                            uidList.add(key);
                            uidList.remove(auth.getUid());
                            Log.d("UserUID",auth.getUid());
                            Log.d("List",uidList.toString());

                    }

                    @Override
                    public void onKeyExited(String key) {
                        uidList.remove(key);
                    }

                    @Override
                    public void onKeyMoved(String key, GeoLocation location) {

                    }

                    @Override
                    public void onGeoQueryReady() {

                    }

                    @Override
                    public void onGeoQueryError(DatabaseError error) {
                        Log.d("GeoError",error.getMessage());

                    }
                });
            }
        });
//asiudhauisdhuasuidas
        }
        catch(SecurityException e){

        }
        return uidList;
    }

I'm questioning whether my set up is correct in terms of having a seperate geofire dir compared to the users and whether the geofire data should be stored within each individual user.

My problem is that if i then try and loop through the UidList in order to get just the uids that are within the geofire query i either seem to get a null return or a return of all the users.

Does anyone have a working solution? End state I would like a List of user objects.


Solution

  • Try the following:

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("GeoFire");
    ref.orderByChild("lattitude").equalTo(lattitude_here).addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
    for(DataSnapshot datas: dataSnapshot.getChildren()){
      String userID=datas.getKey();
        }
     }
     @Override
    public void onCancelled(DatabaseError databaseError) {
       }
    });
    

    Here the snapshot is at GeoFire, then you add a query and iterate to be able to retrieve the ids according to the result of the query.