I need to clear all markers in v2 google map. And again need to add some markers. If anybody knows the answer kindly share your thoughts.
Asked
Active
Viewed 1.6k times
8
-
See http://discgolfsoftware.wordpress.com/2012/12/06/hiding-and-showing-on-screen-markers-with-google-maps-android-api-v2/ – ridoy Jun 19 '13 at 17:44
6 Answers
22
You can either use googleMap.clear()
, or you can store your Markers in a collection of some kind and remove them in a loop:
private ArrayList<Marker> mMarkers;
...
private void removeMarkers() {
for (Marker marker: mMarkers) {
marker.remove();
}
mMarkers.clear();
}

Karakuri
- 38,365
- 12
- 84
- 104
-
clear() will do the job. the second solution will remove from the arraylist and then you will have to update your view/refresh i guess – Raghunandan Jun 19 '13 at 18:29
-
Don't forget to call `mMarkers.clear();` at the end of `removeMarkers` function. – MaciejGórski Jun 19 '13 at 19:41
-
Raghunandan: marker.remove() removes the marker from the map. MaciejGórski: good catch, updated the code. – Karakuri Jun 19 '13 at 22:44
3
ex - if you want to refresh and load new marker point in the map for a button click(in this ex i get button click),
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))
}

Ashana.Jackol
- 3,064
- 28
- 22
2
Use Google Map object and call clear to clear the markers.
mMap.clear();
Check the docs
public final void clear ()
Removes all markers, polylines, polygons, overlays
, etc from the map.

Raghunandan
- 132,755
- 26
- 225
- 256
1
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 is
public static GoogleMap mMap;
That mMap
will initialize automatically inside the
public void onMapReady(GoogleMap googleMap)
method .
There put mMap = googleMap;
Now use the clearOverlays()
method wherever you want.

Bertrand Martel
- 42,756
- 16
- 135
- 159

Sukirti Dash
- 119
- 1
- 5
0
you can use clear() in java and kotlin
java
googleMap.clear()
kotlin
googleMap?.clear()

Rasoul Miri
- 11,234
- 1
- 68
- 78