1

The aim of my application is to retrieve all users current location from real-time firebase and show it on the map in Android Studio. The location of the user is set by using GeoFire and stored in the real-time firebase. The code of setting GeoFire is as follow:

private void settingGeoFire() {
    String firebaseAuth = FirebaseAuth.getInstance().getUid();
    myLocationRef = FirebaseDatabase.getInstance().getReference("UserLocation/"+firebaseAuth);
    geoFire = new GeoFire(myLocationRef);
}

and the structure of the real-time firebase is as follow:

real-time firebase

The question is how to retrieve all user's latitude and longitude which are stored under the file 0 and 1 (refer to the image above) in real-time firebase and show them as multiple markers on the map in android studio? Thanks if you can help!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

1

To get the values of the latitude and longitude from the (0) and (1) nodes, please use the following lines of code:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference userLocationRef = db.child("UserLocation");
userLocationRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot ds : task.getResult().getChildren()) {
                doube lat = ds.child("You").child("l").child("0").getValue(Double.class);
                doube lng = ds.child("You").child("l").child("1").getValue(Double.class);
                Log.d("TAG", lat + ", " + lng); //Check the values

                //Add location on Google Map
                LatLng location = new LatLng(lat, lng);

                map.addMarker(new MarkerOptions()
                        .position(location)
                        .title(lat + ", " + lng)
                        .showInfoWindow();
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Sir, thank you so much, it works! But I have faced 2 problems! 1) May I know can the info window of each user location can be renamed as the user name stored in firebase firestore for differentiating the user? 2) The location (marker) of the users cannot be updated continuously from the real-time firebase. I have to close the activity and reopen the activity to get the location (marker) updated. – Zi Zian Yeo Nov 23 '21 at 16:01
  • 1) Add under each user object in the database, the user name, and then get it in a similar way as you did for the lat and the lng. 2) To do that you have to use a real-time listener and remove the old position once the location is updated. If you have a hard time implementing those features, please post a new question, here on StackOverflow, using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Nov 24 '21 at 07:10
  • Sir, I have used the real-time listener to read the location of multiple users in real-time. But I have no idea how to remove the old markers of the users. The link is the new question that I post, hope you can take a look on it, thanks. (https://stackoverflow.com/questions/70115230/how-to-remove-the-old-marker-of-multiple-updated-location-on-google-map-in-andro) – Zi Zian Yeo Nov 25 '21 at 17:34
  • I'll take a look and if I'll know the answer, I'll write to you. – Alex Mamo Nov 25 '21 at 20:23