在 Android 中清除谷歌地图上的标记

我已经添加了片段活动的映射,并添加了一些标记使用 addMarker 功能,但我能够删除所有标记,我得到通知,为不同的标记列表,

现在我想删除所有的标记,并添加一个新的。

将所有标记保留在 list 中并逐个删除(marker.remove ())的一种方法

有没有更好的办法。

74790 次浏览

If you want to clear "all markers, overlays, and polylines from the map", use ABC0 on your GoogleMap.

If you do not wish to clear polylines and only the markers need to be removed follow the steps below.

First create a new Marker Array like below

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

Then when you add the marker on the google maps also add them to the Marker Array (its AllMarkers in this example)

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


LatLng location = new LatLng(Lat,Long);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(location);
markerOptions.title("Your title");


Marker mLocationMarker = Map.addMarker(markerOptions); // add the marker to Map
AllMarkers.add(mLocationMarker); // add the marker to array


}

then finally call the below method to remove all markers at once

 private void removeAllMarkers() {
for (Marker mLocationMarker: AllMarkers) {
mLocationMarker.remove();
}
AllMarkers.clear();


}

call from anywhere to remove all markers

removeAllMarkers();

I found this solution when i was looking for a way to remove only the map markers without clearing the polylines. Hope this will help you too.