I want to retrieve location markers from firebase database but I can\'t see any markers after running this code. I tried to make EventListener
but the map runs
You need to use a ValueEventListener to obtain data from the Firebase database. This is explained in the documentation.
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
double location_left = dataSnapshot.child("location_left").getValue(Double.class);
double location_right = dataSnapshot.child("location_right").getValue(Double.class);
LatLng sydney = new LatLng(location_left, location_right);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,18));
} else {
Log.e(TAG, "onDataChange: No data");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});