Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear / reset an ItemizedOverlay in Android?

I have a program that is creating an ItemizedOverlay and a map. Everything works fine upon startup.

When you close ande re-open the app, I am using onRestart() to get updated information from my server and update the map. The problem is that at this point, the ItemizedOverlay still contains the old items, and then proceeds to add the new data to the existing data.

I am looking for a way to clear out the ItemizedOverlay. There does not appear to be an ItemizedOveraly.clear, or any similar function.

Ideas about how I can do this / why is it not obvious ?

like image 328
Chris Avatar asked Dec 09 '22 12:12

Chris


1 Answers

A typical custom overlay looks like this. it encapsulates the various OverlayItems displayed on the map in a list.

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem>{

    private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

    public MyItemizedOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));        
    }

    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    public void removeOverlay(OverlayItem overlay) {
        mOverlays.remove(overlay);
        populate();
    }


    public void clear() {
        mOverlays.clear();
        populate();
    }

    @Override
    public int size() {
        return mOverlays.size();
    }
}

Methods can be exposed to add / remove individual overlayitems, but also the remove all overlayitems (clear method).

Remove a single overlayitem

MyItemizedOverlay sitesOverlay =  (MyItemizedOverlay ) map.getOverlays().get(0);
sitesOverlay.removeOverlay(overlay);

Add a single overlayItem

MyItemizedOverlay sitesOverlay =  (MyItemizedOverlay ) map.getOverlays().get(0);
sitesOverlay.addOverlay(new OverlayItem(p, "title", "snippet"));

Remove all overlayItems

MyItemizedOverlay sitesOverlay =  (MyItemizedOverlay ) map.getOverlays().get(0);
sitesOverlay.clear();
like image 120
ddewaele Avatar answered Dec 22 '22 22:12

ddewaele