Skip to content
Advertisement

Displaying data from Firebase into custom Info Window

I’m trying to display a couple of user details from Firebase into a custom Map Marker after the location data has been retrieved from the database but I don’t know if I should implement FirebaseRecyclerAdapter or a normal one. How should I do this?

I’m trying to at least retrieve the user’s name, emergency type, and alert level and display on the Info Window when the marker is clicked:

{
  "Blocked Users" : {
    "RCX2HZXIwlSmMHFgDytf1DgZBgi2" : 0,
    "vqP3H4maEfQxCBHEWqsH9q4O1M52" : 0
  },
  "User Location" : {
    "RCX2HZXIwlSmMHFgDytf1DgZBgi2" : {
      "latitude" : 15.506605,
      "longitude" : 120.86838
    },
    "vqP3H4maEfQxCBHEWqsH9q4O1M52" : {
      "latitude" : 37.3259633,
      "longitude" : -121.898475
    }
  },
  "Users" : {
    "RCX2HZXIwlSmMHFgDytf1DgZBgi2" : {
      "Alert Level" : "High",
      "Emergency Type" : "Natural Disaster",
      "address" : "Lapaz Tarlac",
      "emergencyNum" : "0981232387346",
      "name" : "Rafael Campos",
      "phoneNum" : "0981233445675"
    },
    "vqP3H4maEfQxCBHEWqsH9q4O1M52" : {
      "Alert Level" : "High",
      "Emergency Type" : "Natural Disaster",
      "address" : "Lapaz Tarlac",
      "emergencyNum" : "9876876650987",
      "name" : "Rafael Campos",
      "phoneNum" : "089098786876"
    }
  }
}

Here is my onMapReady():

@Override
    public void onMapReady(@NonNull GoogleMap googleMap) {
        mMap = googleMap;

        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
        DatabaseReference locationRef = rootRef.child("User Location");



        locationRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {


                    LocationSend send = snapshot.getValue(LocationSend.class);
                    LatLng location = new LatLng(send.getLatitude(), send.getLongitude());
                    mMap.addMarker(new MarkerOptions().position(location).title(getCompleteAddress(send.getLatitude(), send.getLongitude())));
                    mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 14F));
                    mMap.setInfoWindowAdapter(new InfoWindowAdapter(Retrieve.this));
                    Log.d("TAG", "latitude, longitude");
                    notification();
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
                LocationSend send = snapshot.getValue(LocationSend.class);
                notification();
            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot snapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }

My adapter:

public class InfoWindowAdapter implements GoogleMap.InfoWindowAdapter{

    Context context;
    public InfoWindowAdapter(Context context){
        this.context = context;
    }

    @Nullable
    @Override
    public View getInfoWindow(@NonNull Marker marker) {
        View infoView = LayoutInflater.from(context).inflate(R.layout.infowindow, null);
        TextView title = infoView.findViewById(R.id.title);
        TextView alert = infoView.findViewById(R.id.alert);
        TextView type = infoView.findViewById(R.id.type);
        title.setText(marker.getTitle());
        alert.setText(marker.getSnippet());
        type.setText(marker.getSnippet());

        return infoView;
    }

    @Nullable
    @Override
    public View getInfoContents(@NonNull Marker marker) {
        return null;
    }
}

Advertisement

Answer

According to your last comment:

In the Users node, the Name, Emergency Type, and Alert Level

To be able to get the values of the indicated fields that correspond to a specific user and add them to a Google Map, please use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userLocationRef = rootRef.child("User Location").child(uid);
userLocationRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> userLocationTask) {
        if (userLocationTask.isSuccessful()) {
            DataSnapshot userLocationSnapshot = userLocationTask.getResult();
            double latitude = userLocationSnapshot.child("latitude").getValue(Double.class);
            double longitude = userLocationSnapshot.child("longitude").getValue(Double.class);

            DatabaseReference uidRef = rootRef.child("Users").child(uid);
            uidRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DataSnapshot> userTask) {
                    if (userTask.isSuccessful()) {
                        DataSnapshot userSnapshot = userTask.getResult();
                        String name = userSnapshot.child("Name").getValue(String.class);
                        String emergencyType = userSnapshot.child("Emergency Type").getValue(String.class);
                        String alertLevel = userSnapshot.child("Alert Level").getValue(String.class);

                        MarkerOptions markerOptions = new MarkerOptions();
                        LatLng latLng = new LatLng(latitude, longitude);
                        markerOptions.position(latLng);
                        markerOptions.title(name + "/" + emergencyType + "/" + alertLevel);
                        googleMap.addMarker(markerOptions);
                    } else {
                        Log.d("TAG", userTask.getException().getMessage()); //Don't ignore potential errors!
                    }
                }
            });
        } else {
            Log.d("TAG", userLocationTask.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

The result of the following code will be the addition of a marker that corresponds to the user location on the Google Map. The title of the marker will contain, the name, the emergency type, and the alert level.

This will indeed work for a single user, if you need to show all users, then simply loop through the DataSnapshot object using getChildren() method like this:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userLocationRef = rootRef.child(uid).child("User Location");
userLocationRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> userLocationTask) {
        if (userLocationTask.isSuccessful()) {
            for (DataSnapshot userLocationSnapshot : userLocationTask.getResult().getChildren()) {
                double latitude = userLocationSnapshot.child("latitude").getValue(Double.class);
                double longitude = userLocationSnapshot.child("longitude").getValue(Double.class);

                DatabaseReference usersRef = rootRef.child("Users").child(uid);
                usersRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DataSnapshot> userTask) {
                        if (userTask.isSuccessful()) {
                            for (DataSnapshot userSnapshot : userTask.getResult().getChildren()) {
                                String name = userSnapshot.child("name").getValue(String.class);
                                String emergencyType = userSnapshot.child("Emergency Type").getValue(String.class);
                                String alertLevel = userSnapshot.child("Alert Level").getValue(String.class);

                                MarkerOptions markerOptions = new MarkerOptions();
                                LatLng latLng = new LatLng(latitude, longitude);
                                markerOptions.position(latLng);
                                markerOptions.title(name + "/" + emergencyType + "/" + alertLevel);
                                googleMap.addMarker(markerOptions);
                            }
                        } else {
                            Log.d("TAG", userTask.getException().getMessage()); //Don't ignore potential errors!
                        }
                    }
                });
            }
        } else {
            Log.d("TAG", userLocationTask.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement