Home > Mobile >  How to retrieve multiple username from Firestore and assign the username to the correct markers on m
How to retrieve multiple username from Firestore and assign the username to the correct markers on m

Time:12-21

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;
    mMap.getUiSettings().setZoomControlsEnabled(true);


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

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

            userLocationRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
                @Override
                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

                               


                                //decrypt
                                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)) {
                                    hm.get(userID).remove();
                                }

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



                            }
                        }
                        catch (Exception e){
                            e.printStackTrace();
                        }

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

        }

        @Override
        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.

CodePudding user response:

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:

Firebase-root
  |
  --- User_Location
       |
       --- $uid
            |
            --- userID: "Jk8i...1jy2"
            |
            --- userName: "Zi Zian Yeo"            
  • Related