Removing Markers in android

 ̄綄美尐妖づ 提交于 2019-12-03 17:20:52

Use this to add markers

As Global

List<Marker> mMarkers = new ArrayList<Marker>();

And In your for loop add markers to this list like

for (int i = 0; i < point_new.length; i++) {

                        MarkerOptions markerOptions = new MarkerOptions();

                            markerOptions.position(point_new[i]);

                        Marker marker = mMap.addMarker(markerOptions);
                               marker.setTitle("Point");
                        marker.setSnippet("this is snippet");
                        marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.p));

                        mMarkers.add(marker); // <-- Like this
}

And to remove markers

private void removeMarkers() {
        for (Marker marker: mMarkers) {
            marker.remove();
        }
        mMarkers.clear();

    }

hope it will help.

Try this,

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

}

Mann

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

Similar to Remove a marker from a GoogleMap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!