thomas2k2k thomas2k2k - 1 month ago 11
Android Question

The remove of individual Marker from Map and List, together

In my application I have an ArrayList markers, exacly:

public ArrayList<Marker> listMarker = new ArrayList<Marker>();
public int p = listMarker.size();
public Marker singleMarker;


I put markers in ArrayList as follows:

MarkerOptions options = new MarkerOptions()
.position(latLng);
listMarker.add(p, singleMarker = nMap.addMarker(options));


I was looking for at site the same problem and I have not found
Now I try to do that every button click will remove the last marker from the map and list. If I have 5 markers that click is 4, the next click is 3 and so on. But not only from the list, and also from the map, the biggest problem

My attempt that does not work like this:

deleteButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

listMarker.remove(k);

}
});


Button is not removed Marker from the map. I suspect that this only removes the last marker from the list but no from the map and do not know how to deal with it.

Answer

Currently you are only removing the marker from the List, the Map is going to hold onto it until you explicitly remove it... Call remove on the Marker to remove it from the map, like so:

deleteButton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
        listMarker.get(k).remove(); // Add this line
        listMarker.remove(k);

    }
});