I am trying to retrieve the latitude and longitude from my Firebase Database but I have difficulties doing it. As you can see under the screenshot, each photos have their own ID which are attached to a user ID. I want to display on a map with a marker all the locations from the database. Attached to this are my code and a screenshot of my database.
Database screenshot:
EDIT: I added more of my code as requested.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener, GoogleMap.OnMarkerClickListener {
private GoogleMap mMap;
private ChildEventListener mChildEventListener;
private DatabaseReference mUsersDataRef;
private DatabaseReference mPhotosDataRef;
private Map<String, User> mUsers = new HashMap<>(); //Map<(String) user-id, (User) user>
private List<Photo> mPhotos = new ArrayList<>();
private boolean usersFetched;
private boolean photosFetched;
private String photo_id;
Marker marker;
Part 2
@Override
protected void onCreate(Bundle savedInstanceState) {
FirebaseApp.initializeApp(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
ChildEventListener mChildEventListener;
mUsersDataRef = FirebaseDatabase.getInstance().getReference("photo_id");
mPhotosDataRef = FirebaseDatabase.getInstance().getReference("users");
mUsersDataRef.push().setValue(marker);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
googleMap.setOnMarkerClickListener(this);
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
mPhotosDataRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot s : dataSnapshot.getChildren()) {
User user = s.getValue(User.class);
mUsers.put(user.getUser_id(), user);
}
usersFetched = true;
doneLoading();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mPhotosDataRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot s : dataSnapshot.getChildren()) {
Photo photo = s.getValue(Photo.class);
mPhotos.add(photo);
}
photosFetched = true;
doneLoading();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void doneLoading() {
if (usersFetched && photosFetched) {
for (Photo photo : mPhotos) {
User thisPhotosUser = mUsers.get(photo.getUser_id());
LatLng location=new LatLng(user.latitude,user.longitude);
mMap.addMarker(new MarkerOptions().position(location).title(user.name)).setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
}
}
}
//@Override
public String getPhoto_id() {
return photo_id;
}
}
Try using Data Snapshot. The code can be modified in this way:
if (usersFetched && photosFetched) {
for (Photo photo : mPhotos) {
User thisPhotosUser = mUsers.get(photo.getUser_id());
thisPhotoUser.addListenerForSingleValueEvent(new ValueEvent Listner()){
public void onDataChange(DataSnapshot datasnapshot){
for(Datasnapshot photos : dataSnapshot.getChildren()){
if(photos.child(thisPhotoUser.exists())){
LatLng location = new LatLng(mPhotosDataRef.child(getPhoto_id(isChild(latitude))),user.longitude);
mMap.addMarker(newMarkerOptions().position(location).title(photo.name)).setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
}
}
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.