在android中删除标记 [英] Removing Markers in android
问题描述
我有一个全局数组对象,像 Marker marker_array [];
,后来在布局中点击我初始化为 marker_array = new Marker [8] ;
。我想添加标记来映射该布局,并在第二次点击时删除,因此我创建了 clickcount
具有零值的全局变量。
I have a Global array Object like Marker marker_array[];
and later in Layout click I initialized it as marker_array = new Marker[8];
. I want to add markers to map on that layout and remove on 2nd click so I created clickcount
Global variable with zero value.
我的正确代码在这里
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个标记但不删除,即使在如果条件,我试图删除标记吐司
显示适当的长度 8
。当我将任何
marker_array
分别作为 marker_array [7]
删除时, Butt 。
The problem is that it creates all 8 markers But does not remove, Even if in if condition where I'm trying to remove markers Toast
shows proper length 8
. Butt when I remove any of the marker_array
separately as marker_array[7]
it removes it.
如何在 marker_array 中删除 map.clear();
方法,因为我还有其他一些东西,例如折线等,我不想删除。
How can I remove all the markers in marker_array without map.clear();
method because I have some other things like polyline etc that I do not want to remove.
任何努力将不胜感激。
Any effort will be appreciated.
推荐答案
使用此功能添加标记
As Global
As Global
List<Marker> mMarkers = new ArrayList<Marker>();
并在您的for循环中将标记添加到此列表中,如
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
}
和删除标记 b>
And to remove markers
private void removeMarkers() {
for (Marker marker: mMarkers) {
marker.remove();
}
mMarkers.clear();
}
希望它能帮上忙。
这篇关于在android中删除标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!