Search code examples
androidfirebasefirebase-realtime-databasegeofire

GeoFire and Firebase useage to locate nearest user


After working through the firebase Git im having some issues, this is my database: database

at the begining i defined

geoFire.setLocation("firebase-hq", new GeoLocation(lat, lang));

edit: after a few fix, Il sharp the issue... Im trying to find all the users in a certain user, after some fixing it does find everyone, but it also finds itself... how can i manage to avoide adding my own user(or delete my own user key from the found users...) here is the code(which hopefully will explain the issue better, since English is not my native language). runnersNearby are the user on this radius...

        mDatabase = FirebaseDatabase.getInstance().getReference();
        mAuth = FirebaseAuth.getInstance();
        double lat = gpsTracker.getLatitude();
        double lang = gpsTracker.getLongitude();
        mDatabase.child("User").child(mAuth.getCurrentUser().getUid()).child("latitude").setValue(lat);
        mDatabase.child("User").child(mAuth.getCurrentUser().getUid()).child("longitude").setValue(lang);
        mDatabase.child("User").child(mAuth.getCurrentUser().getUid()).child("Availability").setValue(true);
        geoFire.setLocation(uid, new GeoLocation(lat, lang));
        GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(lat, lang), 100);
        geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
            @Override
            public void onKeyEntered(String key, GeoLocation location) {
                Toast.makeText(UserProfile.this, "Successfully Found", Toast.LENGTH_SHORT).show();
                runnersNearby.add(key);
                Log.d("Number of users", String.valueOf(runnersNearby.size()));
                Toast.makeText(UserProfile.this, String.valueOf(runnersNearby.size()), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onKeyExited(String key) {
                Toast.makeText(UserProfile.this, "left the place", Toast.LENGTH_SHORT).show();
                runnersNearby.remove(key);
            }

            @Override
            public void onKeyMoved(String key, GeoLocation location) {
                Toast.makeText(UserProfile.this, "key moved but here", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onGeoQueryReady() {
                System.out.println("All initial data has been loaded and events have been fired!");
            }

            @Override
            public void onGeoQueryError(DatabaseError error) {
                Toast.makeText(UserProfile.this, "Error Occured", Toast.LENGTH_SHORT).show();
            }

Solution

  • The problem is, you're referencing the geofire to the root node. You should reference it to a special node for geofire.

    What your database structure should look like

    "User": {
        "uid1": {
            "Availability": true,
        },
        "uid2": {
            "Availability": true,
        }
    },
    "User_Location": {
        "uid1": {
            .. some geofire data ..
        },
        "uid2": {
            .. some geofire data ..
        }
    }
    

    How to reference the geofire to another location

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("User_Location");
    GeoFire geoFire = new GeoFire(ref);
    

    Then set the location as usual

    geoFire.setLocation(userId, new GeoLocation(userLatitude, userLongitude));
    

    After that, the Geofire query should work fine.