Remove a marker from a GoogleMap
Solution 1:
The method signature for addMarker
is:
public final Marker addMarker (MarkerOptions options)
So when you add a marker to a GoogleMap
by specifying the options for the marker, you should save the Marker
object that is returned (instead of the MarkerOptions
object that you used to create it). This object allows you to change the marker state later on. When you are finished with the marker, you can call Marker.remove()
to remove it from the map.
As an aside, if you only want to hide it temporarily, you can toggle the visibility of the marker by calling Marker.setVisible(boolean)
.
Solution 2:
Add the marker to the map like this
Marker markerName = map.addMarker(new MarkerOptions().position(latLng).title("Title"));
Then you'll be able to use the remove method, it will remove only that marker
markerName.remove();