Я пытаюсь показать все транспортное средство на карте, когда какой-либо водитель подключается к сети, поэтому, используя geofire, он работает нормально, но при удалении я не могу удалить на карте, очищается карта, но я не вижу, когда очищать картуиспользовать концепцию asnk.
public static HashMap<String,Marker> markers = new HashMap<>();
synchronized private static void findAllDriverInRadiusForUniqueVehicle(String
selectedVehicleCategory, int limitKm, LatLng srcLocation,String callValue) {
Log.d(TAG, "findAllDriverInRadiusForUniqueVehicle: calling from = "+callValue);
//find driver in radius
GeoFire gf = new GeoFire(DR_DRIVER_LOC_TABLE.child(selectedVehicleCategory));
GeoQuery geoQuery = gf.queryAtLocation(new GeoLocation(srcLocation.latitude, srcLocation.longitude),limitKm);
geoQuery.removeAllListeners();
Log.d(TAG, "findAllDriverInRadiusForUniqueVehicle: driverLocation = " + DR_DRIVER_LOC_TABLE.child(Common.SELECTED_VEHICLE_CATEGORY));
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
Log.d(TAG, "onKeyEntered: i am starting near driver found at location = "+location+", key = "+key);
//use key to get the email of the users
Common.FOUND_DRIVER_LOCATION = location;
Common.driverId = key;
Common.isDriverFound = true;
progressBar.setVisibility(View.INVISIBLE);
txtProgress.setVisibility(View.INVISIBLE);
FirebaseDatabase.getInstance().getReference(Common.user_driver_tbl) //user_rider_tbl
.child(key)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Common.FOUND_DRIVER_IN_LIMIT = dataSnapshot.getValue(Driver.class);
Log.d(TAG, "onDataChange: driver = " + Common.FOUND_DRIVER_IN_LIMIT + ", location = " + location);
if (Common.FOUND_DRIVER_IN_LIMIT != null) {
if (Common.FOUND_DRIVER_IN_LIMIT.getCarType().equalsIgnoreCase(selectedVehicleCategory)) {
// Adding new elements to the HashSet
Common.UNIQUE_VEHICLES.add(dataSnapshot.getKey());
Log.d(TAG, "onDataChange: Common.SELECTED_VEHICLE_CATEGORY = " + selectedVehicleCategory);
addDriverMarker(selectedVehicleCategory,Common.FOUND_DRIVER_IN_LIMIT.getName(), dataSnapshot.getKey(), location);
}
} else {
Common.isDriverFound = false;
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Log.d(TAG, "onKeyEntered: i am exiting");
}
@Override
public void onKeyExited(String key) {
Log.d(TAG, "onKeyExited: driver has exited the radios or driver location is off key = "+key);
Log.d(TAG, "onKeyExited: removing marker key = "+key);
markers.get(key).remove(); //here code is successfully executing but not removing
Log.d(TAG, "onKeyExited: removed marker key = "+key);
Common.homeRadiusAll =Integer.parseInt(context.getString(R.string.home_distance));
findAllDriverInRadiusForUniqueVehicle(selectedVehicleCategory,Common.LIMIT,srcLocation,"2");
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
Log.d(TAG, "onKeyMoved: ="+key);
}
@Override
public void onGeoQueryReady() {
Log.d(TAG, "onGeoQueryReady: is starting distance = " + Common.homeRadiusAll);
if (Common.homeRadiusAll <= Common.LIMIT) //distance just find for 3km
{
Log.d(TAG, "onGeoQueryReady if : distance = " + Common.homeRadiusAll);
}else{
}
Log.d(TAG, "onGeoQueryReady: is ending ");
}
@Override
public void onGeoQueryError(DatabaseError error) {
Log.d(TAG, "onGeoQueryError: no driver found in given query");
}
});
Log.d(TAG, "loadAllAvailableDrivers: Common.FOUND_DRIVER_LOCATION = "+
Common.FOUND_DRIVER_LOCATION);
}
вот метод addDriverMarker, использующий этот метод, я добавляю маркер на карту.
public static void addDriverMarker(String selectedVehicleCategory, String driverName, String
driverKey, GeoLocation location) {
Common.DR_DRIVER_LOC_TABLE.child(selectedVehicleCategory)
.child(driverKey).child("l").
addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
MarkerOptions markerOptions = new MarkerOptions()
.title(driverName)
.snippet("DRIVER ID : " + driverKey)
.flat(true)
.position(new LatLng(location.latitude, location.longitude))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.car3));
marker = Common.mHomeMap.addMarker(markerOptions); //here im adding marker and working
markers.put(driverKey, marker); //here adding to Hashmap
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
//i used everything but unable to delete particular marker from google map on android using java language.