5

I add a marker on each map click, and I only want to see the last one.

I tried the following code:

if(marker!=null)
{
marker.visible(false);
marker=null;
}

marker=new MarkerOptions().position(latLng);
googleMap.addMarker(marker);

I see every marker even though I set it to invisible.

How can I remove after each click? I can't find any remove methods.

Nestor
  • 8,194
  • 7
  • 77
  • 156

2 Answers2

10

Just keep a reference to the marker each time you do the click :

Marker marker = map.addMarker(MARKER_OPTIONS);

Then call remove() method :

marker.remove();
S.Thiongane
  • 6,883
  • 3
  • 37
  • 52
3

As mention here in documentation , Remove() method in Marker class will help you

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/Marker#remove()

and here tutorial about adding and removing markers on map

http://www.jiahaoliuliu.com/2013/08/android-adding-and-removing-markets-on.html

mohammed momn
  • 3,230
  • 1
  • 20
  • 16