我有一个全局数组对象Marker marker_array[];
,稍后在布局中单击我将其初始化为marker_array = new Marker[8];
.我想添加标记以在该布局上进行映射,并在第二次单击时删除,因此我创建clickcount
了零值的全局变量.
我的正确代码在这里
final RelativeLayout layout = (RelativeLayout) findViewById(R.id.track_div); layout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { clickcount++; point_new = new LatLng[8]; point_new[0] = new LatLng(31.5301843, 74.3207487); point_new[1] = new LatLng(31.5214693,74.3236027); point_new[2] = new LatLng(31.5194393, 74.3257327); point_new[3] = new LatLng(31.4942166, 74.3004533); point_new[4] = new LatLng(31.4864646, 74.2911203); point_new[5] = new LatLng(31.4803596, 74.2787933); point_new[6] = new LatLng(31.4764716, 74.2638203); point_new[7] = new LatLng(31.4775236, 74.2628873); // initialize marker_array; marker_array = new Marker[8]; Toast.makeText(getApplicationContext(), "count "+clickcount, Toast.LENGTH_SHORT).show(); // if (clickcount % 2 == 0) { polyline.setVisible(false); for (int i = 0; i < point_new.length; i++){ Toast.makeText(getApplicationContext(), "marker length ="+marker_array.length, Toast.LENGTH_SHORT).show(); marker_array[i].remove(); // marker_array.setVisible(false); } } else { polyline.setVisible(true); for (int i = 0; i < point_new.length; i++) { // marker_array = new Marker[point_new.length]; MarkerOptions markerOptions = new MarkerOptions() .position(point_new[i]); marker_array[i] = mMap.addMarker(markerOptions); marker_array[i].setTitle("Points"); marker_array[i].setSnippet("Distance = 9.6 km, Time = 20 minute/s"); marker_array[i].setIcon(BitmapDescriptorFactory.fromResource(R.drawable.bus)); } }
问题是它创建了所有8个标记但是没有删除,即使在我试图删除标记的情况下Toast
显示正确的长度8
.当我删除任何单独的删除它时,对接.marker_array
marker_array[7]
如何在没有方法的情况下删除marker_array中的所有标记,map.clear();
因为我还有其他一些我不想删除的折线等内容.
任何努力将不胜感激.
用它来添加标记
作为全球
ListmMarkers = new ArrayList ();
并在你的for循环中添加标记到这个列表
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 }
并删除标记
private void removeMarkers() { for (Marker marker: mMarkers) { marker.remove(); } mMarkers.clear(); }
希望它会有所帮助.