问题
So I have a bunch of markers on the map and they get their data from firebase real-time database. I have like a switch button on the map that shows or hides all markers and it works fine. But now I want specific marker to hide when some action occurred to it, but it's not working..!
What really happens is.. when I trigger some action on that marker.. it gets updated on the database normally but it did not hide from map in real-time.! I had to close the map and open it again to see the changes..!
Whats the problem..!?
The Code
Marker studentMarker = null;
ArrayList<Marker> studentsMarkers = new ArrayList<>();
ArrayList<String> stdId = new ArrayList<>();
@Override
public void onChildChanged(DataSnapshot snapshot, String s) {
LatLng latLng = new LatLng(snapshot.getValue(TestUserStudent.class).getLat(),
snapshot.getValue(TestUserStudent.class).getLang());
boolean isShow = snapshot.getValue(TestUserStudent.class).isShow();
if (isShow) {
studentMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title(snapshot.getValue(TestUserStudent.class).getName() + "")
.snippet(snapshot.getValue(TestUserStudent.class).getSection() + "")
.icon(BitmapDescriptorFactory.fromBitmap(
getMarkerBitmapFromView(snapshot.getValue(TestUserStudent.class).getImg(),
R.drawable.map_marker_red))));
studentsMarkers.add(studentMarker);
stdId.add(snapshot.getKey());
}
}
// this method called when I click on button inside the infowindow of a marker from the list and it updates the marker in the database correctly!
void onWindowInfoButtonClicked(Marker marker) {
for (int i = 0; i < studentsMarkers.size(); i++) {
if (marker.getId().equalsIgnoreCase(studentsMarkers.get(i).getId())) {
studentsMarkers.get(i).remove();
stdId.remove(i);
final String id = stdId.get(i);
HashMap<String, Object> result = new HashMap<>();
result.put("show", false);
FirebaseDatabase.getInstance().getReference().child("Students").child(id)
.updateChildren(result);
}
}
}
// I use this to either show or hide all markers.. and it works fine no problem here
void updateShowing(final boolean show) {
for (int i = 0; i < studentsMarkers.size(); i++) {
studentsMarkers.get(i).remove();
}
FirebaseDatabase.getInstance().getReference().child("Students")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot ds : snapshot.getChildren()) {
ds.child("show").getRef().setValue(show);
}
}
@Override
public void onCancelled(DatabaseError error) {
Toast.makeText(getActivity(), "" + error.getMessage(), Toast.LENGTH_SHORT).show();
Log.i("onCancelled: ", error.getDetails() + "\n " + error.getMessage());
}
});
}
回答1:
You should do it in your onWindowInfoButtonClicked() method. Change this line studentsMarkers.get(i).remove();
to studentsMarkers.get(i).setVisible(false);
.
Also, I don't understand your logic of
stdId.remove(i);
final String id = stdId.get(i);
You removed the item at position i
but then you try to access a new item at this position i
and update its value?
And you may simplify your method void onWindowInfoButtonClicked(Marker marker)
as below. Since you will pass in the targeted Marker object to this function already, there is no need to iterate through your studentMarkers
list to get back the Marker object.
void onWindowInfoButtonClicked(Marker marker) {
marker.setVisible(false);
String id = marker.getId();
HashMap<String, Object> result = new HashMap<>();
result.put("show", false);
FirebaseDatabase.getInstance().getReference().child("Students").child(id)
.updateChildren(result);
}
The method above will only hide the marker but not delete it, since your title stated "Hiding".
EDIT
You can set onMarkerClick(Marker marker) instead of onWindowInfoButtonClicked(Marker marker).
public boolean onMarkerClick(final Marker marker) {
marker.setVisible(false);
String id = marker.getId();
HashMap<String, Object> result = new HashMap<>();
result.put("show", false);
FirebaseDatabase.getInstance().getReference().child("Students").child(id)
.updateChildren(result);
return false;
}
Remember to set your map to listen to this listener
yourMap.setOnMarkerClickListener(this);
来源:https://stackoverflow.com/questions/49168472/hiding-just-one-marker-from-a-list-isnt-working