Need to retrieve Latitude and Longitude for each specific event from Firebase where the user when creating en event also picks event location (Latitude and Longitude) by setting marker at a specific location.
The problem that I am having is that the last event saved overrides all other existing event locations and so when you hit the TextView to take you to GoogleMaps to show you where the event will be taking place only last event's location saved comes up. But obviously each event has its own specific location.
For example, if I create three events and their locations are Rome, Berlin, and Moscow, the only marker that will be created on GoogleMap will be of event in Moscow because that was the last location saved.
I would like for when the second user hits the TextView using each event-specific postid for that event to be found in the database and for its specific coordinates to show up on the map. Can someone please help me to achieve this? I have been trying to achieve this with datasnapshot, but I haven't been able to find other event locations. I always just get the last event's location saved for all posts. Is there a better way to do it than datasnapshot?
Picture below hope helps clear up how my data structure is organized.
MapsActivityUser.java
public class MapsActivityUser extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps_user);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
final DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Post post = snapshot.getValue(Post.class);
if (post != null) {
double latitude = post.getLocation().getLatitude();
double longitude = post.getLocation().getLongitude();
LatLng location = new LatLng(latitude, longitude);
map.addMarker(new MarkerOptions().position(location).title("Event location"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 10));
} else {
Toast.makeText(MapsActivityUser.this, "Event doesn't have location on Map", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}