In the above tree I have stored the current latitude and longitude of the driversAvailable. If there are multiple drivers present under driversAvailable tree then how can I show all of them on the map? I have written following code till now and it isn't working:
private DatabaseReference driverLoadLocationRef;
private ValueEventListener driverLoadLocationRefListener;
private void loadAllAvailableDrivers() {
DatabaseReference driverLoading = FirebaseDatabase.getInstance().getReference().child("driversAvailable");
GeoFire geoDriver = new GeoFire(driverLoading);
GeoQuery geoQueryDriver = geoDriver.queryAtLocation(new GeoLocation(mLastLocation.getLatitude(),mLastLocation.getLongitude()),distance);
geoQueryDriver.removeAllListeners();
geoQueryDriver.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(final String key, GeoLocation location) {
if (!driverLoad ) {
driverLoad = true;
driverLoadId = key;
driverLoadLocationRef = FirebaseDatabase.getInstance().getReference().child("driversAvailable").child(driverFoundId).child("l");
driverLoadLocationRefListener = driverLoadLocationRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists() ) //If doesnot exist check then app will crash
{
List<object> map = (List<object>) dataSnapshot.getValue();
double locationLoadLat = 0;
double locationLoadLng = 0;
if (map.get(0) != null)
{
locationLoadLat = Double.parseDouble(map.get(0).toString());
}
if (map.get(1) != null)
{
locationLoadLng = Double.parseDouble(map.get(1).toString());
}
LatLng driverLoadLatLng = new LatLng(locationLoadLat,locationLoadLng);
if (mDriverMarker != null) //If not added app crash since it will try to remove which doesnot exist
{
mDriverMarker.remove();
}
mDriverMarker = mMap.addMarker(new MarkerOptions().position(driverLoadLatLng).flat(true)
.title(driverLoadId)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.car3)));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(PassengerMapsActivity.this,"Failed",Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public void onKeyExited(String key) {
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
if (distance <= LIMIT)
{
distance++;
loadAllAvailableDrivers();
}
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
});
}