Remove Token from GoogleMap

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

My solution is to save the markers on the map and redraw the map when I want to remove the marker, but this 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?

+58
android google-maps
Dec 03
source share
8 answers

Method signature for addMarker :

 public final Marker addMarker (MarkerOptions options) 

Therefore, when you add a marker to GoogleMap by setting marker parameters, you must save the returned Marker object (instead of the MarkerOptions object that you used to create it). This object allows you to subsequently change the state of the marker. When you are done with the marker, you can call Marker.remove() to remove it from the map.

As an aside, if you want to temporarily hide it, you can switch the marker's visibility by calling Marker.setVisible(boolean) .

+156
Dec 03 '12 at 22:23
source share

to clear all scribbles on the map use

 map.clear() 
+15
Oct 07 '14 at 22:20
source share

Add a marker to the map like this.

 Marker markerName = map.addMarker(new MarkerOptions().position(latLng).title("Title")); 

Then you can use the remove method, it will only remove this marker

 markerName.remove(); 
+11
Dec 25 '15 at 9:20
source share

1. If you want to remove the marker, you can do it like marker.remove(); you can also hide the marker instead of deleting it as

  marker.setVisible(false); 

and make it visible later when necessary.
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 do not want to delete Polylines, Circles , etc., than to use a loop for the length of the marker (if you have several markers), to remove those Check from the example here OR to set them Visible false AND not use map.clear(); in this case.

+1
Dec 09 '15 at 4:54
source share

if the marker exists, delete the last marker. if the marker does not exist, create the 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))); } 
+1
Jul 07 '16 at 7:08
source share

use the following code:

  mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { marker.remove(); return true; } }); 

as soon as you click the marker , it can be deleted.

+1
01 Sep '17 at 13:53 on
source share

Make global variable to track marker

 private Marker currentLocationMarker; 

// Delete the old marker

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

// Add updated marker and move 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
Jan 11 '17 at 8:50
source share

Create an array with all the markers to add to the map.

Later use:

 Marker temp = markers.get(markers.size() - 1); temp.remove(); 
-one
May 28 '15 at 23:19
source share



All Articles