0

I'm using OSMDroid and have a bunch of different markers and trying to toggle marker overlays on/off using a checkbox setup. I grab a json array and get my data to create my markers as well as their gps location. All that works great.

I can show the markers fine using:

mapView.getOverlays().add(overlay);

but when I try to reverse it using:

mapView.getOverlays().remove(overlay);

Sadly, it only removes the last placed marker and not all of them. Please tell me where my failure lies. Thanks!

EDIT Additional code:

List<OverlayItem> itemList = new ArrayList<OverlayItem>();
GeoPoint geoPoint = new GeoPoint(lat, lng);
overlayItem = new OverlayItem("Title", "Snippet", geoPoint);

overlayItem.setMarker(getResources().getDrawable(R.drawable.marker));

itemList.add(overlayItem);

overlay = new ItemizedOverlayWithFocus<OverlayItem>(getApplicationContext(), itemList, 
        new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {

            @Override
            public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
            // This will do something eventually
            return true;
            }

            @Override
            public boolean onItemLongPress(final int index, final OverlayItem item) {
            // This will do something eventually
            return false;
            }
        });

mapView.getOverlays().add(overlay);

SOLUTION (This worked for me)

ArrayList<Marker> myMarkers= new ArrayList<>();

Marker myMarker = new Marker(mapView);
                    myMarker.setPosition(new GeoPoint(lat, lng));
                    myMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
                    myMarker.setTitle(TITLE);
                    myMarker.setIcon(getResources().getDrawable(R.drawable.myMarker));
                    myMarker.setSnippet(SNIPPET);
                    mapView.getOverlays().add(myMarker);
                    myMarkers.add(myMarker);

Then I was able to toggle ALL markers off with:

for (Marker m : myMarkers) {
     mapView.getOverlays().remove(m);
     }
WizKiddB
  • 3
  • 1
  • 4

1 Answers1

0

You haven't shown enough code to be sure, but I guess, your myMarker variable holds a reference to the last added marker and so you remove only the last one.

You should keep references to all your markers (e.g. in List) and go through them when removing.

Or you may use FolderOverlay for grouping and easy removal of all markers.

Josef Adamcik
  • 5,620
  • 3
  • 36
  • 42
  • I've made some additional edits based on your comments. Thanks! – WizKiddB Jan 23 '18 at 20:22
  • I now have everything in a List, as you advised. Following what I found at https://stackoverflow.com/a/41090960/825452. Now it adds the markers (as a list I guess?) but doesn't remove any when I reverse it again. – WizKiddB Jan 24 '18 at 02:40
  • @WizKiddB So edit your question again and add updated version of your code. It's nearly impossible to guess where you have a mistake in your code. – Josef Adamcik Jan 24 '18 at 11:15
  • Sorry. I thought it updated. I guess I was wrong. It's updated now. – WizKiddB Jan 24 '18 at 16:37
  • After further research based upon your answer, you were correct sir. Much appreciated. Will update question with full solution. Thanks! – WizKiddB Jan 28 '18 at 05:54