The aim of my application is to retrieve all users current location from real-time firebase and show it on the map in Android Studio. The location of the user is set by using GeoFire and stored in the real-time firebase. The code of setting GeoFire is as follow:
private void settingGeoFire() {
String firebaseAuth = FirebaseAuth.getInstance().getUid();
myLocationRef = FirebaseDatabase.getInstance().getReference("UserLocation/"+firebaseAuth);
geoFire = new GeoFire(myLocationRef);
}
and the structure of the real-time firebase is as follow:
The question is how to retrieve all user's latitude and longitude which are stored under the file 0 and 1 (refer to the image above) in real-time firebase and show them as multiple markers on the map in android studio? Thanks if you can help!
To get the values of the latitude and longitude from the (0) and (1) nodes, please use the following lines of code:
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference userLocationRef = db.child("UserLocation");
userLocationRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
for (DataSnapshot ds : task.getResult().getChildren()) {
doube lat = ds.child("You").child("l").child("0").getValue(Double.class);
doube lng = ds.child("You").child("l").child("1").getValue(Double.class);
Log.d("TAG", lat + ", " + lng); //Check the values
//Add location on Google Map
LatLng location = new LatLng(lat, lng);
map.addMarker(new MarkerOptions()
.position(location)
.title(lat + ", " + lng)
.showInfoWindow();
}
} else {
Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
}
}
});