How to clear all the markers in v2 google map?

2019-06-16 20:01发布

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.

6条回答
别忘想泡老子
2楼-- · 2019-06-16 20:40

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.

查看更多
老娘就宠你
3楼-- · 2019-06-16 20:44

you can use clear() in java and kotlin

java

 googleMap.clear()

kotlin

 googleMap?.clear()
查看更多
爷、活的狠高调
4楼-- · 2019-06-16 20:46

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();
}
查看更多
霸刀☆藐视天下
5楼-- · 2019-06-16 20:51

I think this would be helpful for you. Take all the markers in a List and refresh the map view whenever you need to replace markers by clearing the object of Google Map and List variable.

查看更多
ら.Afraid
6楼-- · 2019-06-16 20:54

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

mMap.clear();

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

Check the docs

public final void clear ()

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

查看更多
淡お忘
7楼-- · 2019-06-16 21:03

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

}
查看更多
登录 后发表回答