How to clear / reset an ItemizedOverlay in Android?

前端 未结 2 1857
遇见更好的自我
遇见更好的自我 2021-02-03 15:34

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 u

相关标签:
2条回答
  • 2021-02-03 16:07

    You create your own ItemizedOverlay sublcass when using a MapView. You can add a clear() method if you want.

    0 讨论(0)
  • 2021-02-03 16:28

    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();
    
    0 讨论(0)
提交回复
热议问题