143

In the new Google Maps API for Android, we can add a marker, but there is no way to (easily) remove one.

My solution is to keep the markers in a map and redraw the map when I want to remove a marker, but it is not very efficient.

private final Map<String, MarkerOptions> mMarkers = new ConcurrentHashMap<String, MarkerOptions>();

private void add(String name, LatLng ll) {
  final MarkerOptions marker = new MarkerOptions().position(ll).title(name);
  mMarkers.put(name, marker);

  runOnUiThread(new Runnable() {
    @Override
    public void run() {
      mMap.addMarker(marker);
    }
  });
}

private void remove(String name) {
  mMarkers.remove(name);

  runOnUiThread(new Runnable() {
    @Override
    public void run() {
      mMap.clear();

      for (MarkerOptions item : mMarkers.values()) {
        mMap.addMarker(item);
      }
    }
  });
}

Does anyone have a better idea?

3
  • Why do you use a Runnable() to add the markers? Commented Mar 3, 2017 at 10:07
  • To do it on the UI thread Commented Mar 3, 2017 at 14:45
  • use mMap.clear() to clear all markers Commented Dec 1, 2018 at 20:56

13 Answers 13

282

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).

2
  • 1
    This is not working for me. marker.setVisible(false), marker.remove(), even markerManager.remove(marker) is not affecting the map. Markers still remains. any hints?
    – Totalys
    Commented Mar 17, 2016 at 15:14
  • 1
    See my comment below about holding onto a marker, while it holds little use after it has been removed from the map, if you should decide to examine its values.. the Remove method sets the tag value to null...
    – Speckpgh
    Commented Jan 25, 2018 at 5:34
42

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();
36

to clear all scribbles in the map use

map.clear()
1
  • 20
    This clears all overlays on the map, not just a marker as the question suggests.
    – Emzor
    Commented Jan 30, 2016 at 8:51
8

If you use Kotlin language you just add this code:

Create global variables of GoogleMap and Marker types.

I use variable marker to make variable marker value can change directly

private lateinit var map: GoogleMap
private lateinit var marker: Marker

And I use this function/method to add the marker on my map:

private fun placeMarkerOnMap(location: LatLng) {
    val markerOptions = MarkerOptions().position(location)
    val titleStr = getAddress(location)
    markerOptions.title(titleStr)
    marker = map.addMarker(markerOptions)
}

After I create the function I place this code on the onMapReady() to remove the marker and create a new one:

map.setOnMapClickListener { location ->
        map.clear()
        marker.remove()
        placeMarkerOnMap(location)
    }

It's bonus if you want to display the address location when you click the marker add this code to hide and show the marker address but you need a method to get the address location. I got the code from this post: How to get complete address from latitude and longitude?

map.setOnMarkerClickListener {marker ->
        if (marker.isInfoWindowShown){
            marker.hideInfoWindow()
        }else{
            marker.showInfoWindow()
        }
        true
    }
1
  • The marker.remove() or google.clear() removes all the marker which are not being updated it only shows the updated marker after clearing the map or removing the marker
    – Dkathayat1
    Commented Mar 5, 2023 at 7:10
6

if marker exist remove last marker. if marker does not exist create current marker

Marker currentMarker = null;
if (currentMarker!=null) {
    currentMarker.remove();
    currentMarker=null;
}

if (currentMarker==null) {
    currentMarker = mMap.addMarker(new MarkerOptions().position(arg0).
    icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
}
4

Just a NOTE, something that I wasted hours tracking down tonight...

If you decide to hold onto a marker for some reason, after you have REMOVED it from a map... getTag will return NULL, even though the remaining get values will return with the values you set them to when the marker was created...

TAG value is set to NULL if you ever remove a marker, and then attempt to reference it.

Seems like a bug to me...

1
  • Not sure it's related, but I have my markers in a hashmap and if I call marker.remove() and then markers.clear(), the hashmap gets cleared, but the markers stay on the map. The code works if I remove markers.clear()
    – Simon Tran
    Commented Apr 19, 2023 at 19:30
2

Make a global variable to keep track of marker

private Marker currentLocationMarker;

//Remove old marker

            if (null != currentLocationMarker) {
                currentLocationMarker.remove();
            }

// Add updated marker in and move the camera

            currentLocationMarker = mMap.addMarker(new MarkerOptions().position(
                    new LatLng(getLatitude(), getLongitude()))
                    .title("You are now Here").visible(true)
                    .icon(Utils.getMarkerBitmapFromView(getActivity(), R.drawable.auto_front))
                    .snippet("Updated Location"));

            currentLocationMarker.showInfoWindow();
0
2

use the following code:

 mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
       @Override
       public boolean onMarkerClick(Marker marker) {

           marker.remove();
           return true;
       }
   });

once you click on "a marker", you can remove it.

1

Create array with all markers on add in map.

Later, use:

Marker temp = markers.get(markers.size() - 1);
temp.remove();
0
1

1. If you want to remove a marker you can do it as marker.remove(); alternatively you can also hide the marker instead of removing it as

 marker.setVisible(false);

and make it visible later whenever needed.
2. However if you want to remove all markers from the map Use map.clear();
Note: map.clear(); will also remove Polylines, Circles etc.
3. If you not want to remove Polylines, Circles etc. than use a loop to the length of marker (if you have multiple markers) to remove those Check out the Example here OR set them Visible false And do not use map.clear(); in such case.

1

For those that are following the example on this GoogleMaps - MapWithMarker project, you may remove the marker by doing so

override fun onMapReady(googleMap: GoogleMap?) {
    googleMap?.apply {

        // Remove marker
        clear()

        val sydney = LatLng(-33.852, 151.211)
        addMarker(
            MarkerOptions()
                .position(sydney)
                .title("Marker in Sydney")
        )
        moveCamera(CameraUpdateFactory.newLatLng(sydney))
    }
}
1

Try this, it is updating the current location, and it works fine.

        public void onLocationChanged(@NonNull Location location) {               
        //here we update the location on the map
        
        LatLng myActualLocation = new LatLng(location.getLatitude(), location.getLongitude());
        
        if (markerName!=null){  // marker name is declared as a gloval variable.
            markerName.remove();
        }
        
        markerName = mMap.addMarker(new MarkerOptions().position(myActualLocation).title("Marker Miami").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
        
        // mMap.addMarker(new MarkerOptions().position(myActualLocation).title("Marker Miami").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myActualLocation,18));
        
        }
1

In Kotlin

global variable

private var marker: Marker? = null

in the function

private fun placeMarkerOnMap(location: LatLng) {
        val markerOptions = MarkerOptions()
           .position(location)
           .title("My Location")
        marker = mMap.addMarker(markerOptions)
    }

when click

mMap.setOnMapClickListener { location ->
                mMaps.clear()
                marker?.remove()
                placeMarkerOnMap(location)
            }

Not the answer you're looking for? Browse other questions tagged or ask your own question.