90

I have added map on fragment activity and added several marker using addMarker function, but i am able to remove all markers , I am getting notification for different list of markers,

Now i wants to remove all markers and add new one.

one way to keep all markers in list and remove one by one, (marker.remove())

Is there any better way to clear all marker.

Ashish Kasma
  • 3,594
  • 7
  • 26
  • 29

2 Answers2

213

If you want to clear "all markers, overlays, and polylines from the map", use clear() on your GoogleMap.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Should I call remove() on all of them before doing that? I mean, if I still have an ArrayList with those Markers, I suppose I'm still having an active reference to those Markers, so they will remain on memory even if I clear the map. Am I right about this or clearing the map wipes the references to the Markers? – Charlie-Blake Mar 27 '13 at 16:24
  • 5
    @santirivera92: "Should I call remove() on all of them before doing that?" -- AFAIK, you should not need to. "if I still have an ArrayList with those Markers, I suppose I'm still having an active reference to those Markers, so they will remain on memory even if I clear the map" -- yes, but that is *your* code, not Google's. If *you* hold onto `Marker` objects separately from the map, *you* need to let go of those. `clear()` will remove them *visually*. – CommonsWare Mar 27 '13 at 16:37
  • Of course. So, clearing my ArrayLists and then clearing the map will get rid of them forever? – Charlie-Blake Mar 27 '13 at 16:59
  • 1
    @santirivera92: Yes, it should. Nothing else should be holding on to them. – CommonsWare Mar 28 '13 at 11:43
  • 8
    Also note that `clear()` function won't reset the `Marker` IDs. Basically you won't get `m0` again for new `Marker` adding. – Prasad De Zoysa Aug 15 '13 at 03:22
  • 2
    @Prasad Is there any way to get m0 again? – Gokhan Arik Sep 07 '16 at 22:56
  • However `clean` will also remove the toolbar (direction etc.) from the map. That's why the method is named `clean`. – xinthink Nov 25 '16 at 10:02
7

If you do not wish to clear polylines and only the markers need to be removed follow the steps below.

First create a new Marker Array like below

List<Marker> AllMarkers = new ArrayList<Marker>();

Then when you add the marker on the google maps also add them to the Marker Array (its AllMarkers in this example)

for(int i=0;i<places.length();i++){

                LatLng location = new LatLng(Lat,Long);
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(location);
                markerOptions.title("Your title");

                 Marker mLocationMarker = Map.addMarker(markerOptions); // add the marker to Map
                    AllMarkers.add(mLocationMarker); // add the marker to array

                }

then finally call the below method to remove all markers at once

 private void removeAllMarkers() {
        for (Marker mLocationMarker: AllMarkers) {
            mLocationMarker.remove();
        }
        AllMarkers.clear();

    }

call from anywhere to remove all markers

removeAllMarkers();

I found this solution when i was looking for a way to remove only the map markers without clearing the polylines. Hope this will help you too.