80

The only method that removes markers from map is clear. However it clears all markers from the map.

I want to remove only single marker or group of markers.

How could i achieve this?

Alexey Zakharov
  • 24,694
  • 42
  • 126
  • 197

3 Answers3

211

After adding the marker it is possible to obtain its reference:

Marker marker = map.addMarker(..);

The Marker class has a remove method:

JJD
  • 50,076
  • 60
  • 203
  • 339
Alexey Zakharov
  • 24,694
  • 42
  • 126
  • 197
19

I got the same problem, so to fix it I'm doing

mMap = super.getMap();
map.clear();
douarbou
  • 2,283
  • 1
  • 21
  • 25
  • 47
    This removes all overlays of the map not a single marker as the question specifies. As stated in the docs: `public final void clear () Removes all markers, polylines, polygons, overlays, etc from the map.`[link](https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap#clear()) – clauziere Aug 07 '13 at 19:48
  • 1
    Maybe clean and simple but not the right answer. Right answer is above. Marker.remove() – Cocorico Mar 13 '15 at 15:55
  • 1
    It is never the right answer. It simply removes all markers, not a given one. – Tom Apr 13 '17 at 10:32
15

I wrote up a blog post on how to remove Markers when they are moved off the screen, and adding them again when they are on the screen. This is useful if you are trying to add thousands of Markers to a GoogleMap at the same time but don't want the performance to suffer as much as it would if they are all on the map at the same time. It uses the same method you detailed (calling remove() on a Marker).

Hiding and Showing on screen Markers with Google Maps Android API V2

DiscDev
  • 38,652
  • 20
  • 117
  • 133