In my database, I have values about location for each persons like that database structure and I want to retrieve data and show in the map. How get get and show? I created an ArrayList
and details class constuctor for get a values.
Advertisement
Answer
To solve this, please use the following code:
JavaScript
x
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference locationsRef = rootRef.child("Locations");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String latitude = ds.child("Latitude").getValue(String.class);
String longitude = ds.child("Longitude").getValue(String.class);
LatLng latLng = new LatLng(String.valueOf(latitude), String.valueOf(longitude));
MarkerOptions markerOptions = new MarkerOptions().position(latLng).title("My Marker");
Marker marker = googleMap.addMarker(markerOptions);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
locationsRef.addListenerForSingleValueEvent(valueEventListener);
But I strongly recommend you, to store the values for the latitude
and longitude
as doubles
and not as strings.