How to clear all markers in google v2 map?

I need to clear all markers in google v2 map. And again, you need to add some markers. If anyone knows the answer, share your thoughts.

+7
source share
5 answers

You can use googleMap.clear() , or you can store your markers in some kind of collection and delete them in a loop:

 private ArrayList<Marker> mMarkers; ... private void removeMarkers() { for (Marker marker: mMarkers) { marker.remove(); } mMarkers.clear(); } 
+22
source

ex - if you want to update and load a new marker point on the map to press the button (in this case, click the "OK" button),

  switch ( view.getId() ) { case R.id.buttonOne: //clear googlemap googleMap.clear(); //call to generate new marker this.getMarker(lat,lang); break; } //to add new marker public void getMarker ( String lat,String lang ) { LatLng latLang = new LatLng( lat, lang); //call to your googlemap implementation method this.getGoogleMap(); Marker marker = googleMap.addMarker(new MarkerOptions().position(latLang)) } 
+2
source

Use the Google Map object and clear markers to clear markers.

 mMap.clear(); 

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap#clear ()

Check documents

public final void clear ()

Removes all markers, polylines, polygons, overlays , etc. from the map.

+1
source

just create a method as clearOverlays()

and inside the method

 public void clearOverlays(){ if(mMap!=null){ mMap.clear(); }else{ Log.d("Maps::","mMap is null"); } } 

where mMap

 public static GoogleMap mMap; 

That mMap will be automatically initialized inside public void onMapReady(GoogleMap googleMap) .

This is where mMap = googleMap;

Now use the clearOverlays() method clearOverlays() you want.

+1
source

I think this one would be useful for you. Take all the markers in the list and refresh the map display when you need to replace the markers by clearing the object from the Google map and the list.

0
source

All Articles