Skip to content

How to retrieve multiple username from Firestore and assign the username to the correct markers on map

The code shown below is the code used to retrieve the multiple users location from Realtime Firebase and assign to the markers on map:

public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    DatabaseReference db = FirebaseDatabase.getInstance().getReference();
    DatabaseReference userLocationRef = db.child("User_Location");

    db.addValueEventListener(new ValueEventListener() {
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            userLocationRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
                public void onComplete(@NonNull Task<DataSnapshot> task) {
                    if (task.isSuccessful()) {

                        try {
                            for (DataSnapshot ds : task.getResult().getChildren()) {
                                String userID = String.valueOf(ds.child("userID").getValue());

                                String encryptedLatitude = ds.child("You").child("l").child("0").getValue(String.class);
                                String  encryptedLongitude = ds.child("You").child("l").child("1").getValue(String.class);
                                Log.d("encryptedLocation", encryptedLatitude + ", " + encryptedLongitude); //Check the values
                                Log.d("userid", userID); //Check the values


                                LocationEncryption locationEncryption = new LocationEncryption();
                                String decryptedLatitude = null;
                                String decryptedLongitude = null;

                                decryptedLatitude = locationEncryption.decrypt(encryptedLatitude);
                                decryptedLongitude = locationEncryption.decrypt(encryptedLongitude);

                                Log.d("Decrypted", decryptedLatitude + ", " + decryptedLongitude); //Check the values

                                double lat = Double.valueOf(decryptedLatitude);
                                double lng = Double.valueOf(decryptedLongitude);

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

                                if (hm.containsKey(userID)) {

                                currentLocationMarker = mMap.addMarker(new MarkerOptions().position(location).title(userID));
                                hm.put(userID, currentLocationMarker);

                        catch (Exception e){

                    } else {
                        Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!


        public void onCancelled(@NonNull DatabaseError error) {


The structure of the real-time firebase is as follow: realtime firebase

The result of the code is as follow: result

When I click on the marker, it only shows the userID of the user. I want the marker to show the name of the user together with the userID. But the problem is that the multiple users’ names are stored in Firestore.

The structure of the Firestore is as follow: Firestore firestore The admin details and the user details are stored in the same collection “Users”. The fields “isAdmin” and “isUser” are the method to differentiate them. I only need the username and the admin name is not required.

So, how to retrieve multiple users’ names from Firestore and assign the names to the correct markers on the map.


When I click on the marker, it only shows the userID of the user. I want the marker to show the name of the user together with the userID.

The best option that you have is to store the name of the user right near the userID. There is no need to create another database call to only get the name of the user. So your new database schema should look like this:

  --- User_Location
       --- $uid
            --- userID: "Jk8i...1jy2"
            --- userName: "Zi Zian Yeo" 👈

And in code should look like this:

String userID = ds.child("userID").getValue(String.class);
String userName = ds.child("userName").getValue(String.class);

And to add these values as a title of the marker please use the following lines of code:

String title = userID + "/" + userName;
currentLocationMarker = mMap.addMarker(new MarkerOptions().position(location).title(title));