我有一个全局数组对象,如 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个标记,但不会删除它们,即使在我试图删除标记的if条件中,
Toast
显示适当的长度8
。但是当我分别删除任何一个 marker_array
如 marker_array[7]
时,它会将其删除。如何在不使用map.clear();
方法的情况下删除marker_array 中的所有标记,因为我还有其他东西,如折线等,我不想删除。任何努力都将不胜感激。