I'm new to Android, I followed this tutorial: https://codelabs.developers.google.com/codelabs/realtime-asset-tracking/index.html#0
for real-time tracking, all right, my question is how do I delete a marker from the map when it is deleted in firebase, the code is as follows:
private void subscribeToUpdates() {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("locations").child("unidad_1");
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
setMarker(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
setMarker(dataSnapshot);
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
removeMarker(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError error) {
Log.d(TAG, "Failed to read value.", error.toException());
}
});
}
private void removeMarker(DataSnapshot dataSnapshot) {
String key = dataSnapshot.getKey();
HashMap<String, Object> value = (HashMap<String, Object>) dataSnapshot.getValue();
if (!mMarkers.containsKey(key)) {
mMarkers.remove(key);
}
}
private void setMarker(DataSnapshot dataSnapshot) {
// When a location update is received, put or update
// its value in mMarkers, which contains all the markers
// for locations received, so that we can build the
// boundaries required to show them all on the map at once
String key = dataSnapshot.getKey();
HashMap<String, Object> value = (HashMap<String, Object>) dataSnapshot.getValue();
double lat = Double.parseDouble(value.get("latitude").toString());
double lng = Double.parseDouble(value.get("longitude").toString());
LatLng location = new LatLng(lat, lng);
if (!mMarkers.containsKey(key)) {
mMarkers.put(key, mMap.addMarker(new MarkerOptions().title(key).position(location)));
} else {
mMarkers.get(key).setPosition(location);
}
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker marker : mMarkers.values()) {
builder.include(marker.getPosition());
}
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 0));
}
What is the correct way to make this work? Thanks in advance!
In your removeMarker()
method change your code to look like this:
if (mMarkers.containsKey(key)) {
Marker marker = mMarkers.remove(key);
if(marker != null){
marker.remove();
}
}
I am assuming that mMarkers
is a HashMap
. In which case removing the entry with remove(key)
will remove the entry from your HashMap
and return the Marker
(or null if it doesn't exist). Then just use the remove
method of the marker to remove it from the map.
Another problem with your code is you check if the HashMap
does NOT contain the key. So that code would probably not execute.
EDIT
You may have confused mMarker
the HashMap
with the Marker
of the Map
. "mMarker" is not a good name for coding -- as it is not obvious. You should change it to something more obvious like "mMarkerMap".