gpt4 book ai didi

java - 折线未清除

转载 作者:行者123 更新时间:2023-12-02 02:15:36 25 4
gpt4 key购买 nike

尽管使用了 setVisibility(false),多段线仍未清除

我们创建了一个折线数组列表,并在添加新折线时添加到其中。但我们想在不清除 map 的情况下清除所有折线,并没有成功。我们尝试使用折线数组来使用 setPoints 选项,但我们无法从数组列表中检索所需的折线。它总是给出 IndexOutOfBounds 错误。

ArrayList<Polyline> polylines=new ArrayList<Polyline>();

ArrayList points = null;
Polyline p=null;

int counter=0;
for (int i = 0; i < result.size(); i++) {
counter++;
points = new ArrayList();
lineOptions = new PolylineOptions();
trial1=new ArrayList<ArrayList<LatLng>>();

List<HashMap<String, String>> path = result.get(i);

for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);

double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);

points.add(position);
}
trial1.add(points);
lineOptions.addAll(points);
lineOptions.width(12);
lineOptions.color(Color.RED);
lineOptions.geodesic(true);
lineOptions.clickable(true);


if(points.size()!=0 && points!=null && lineOptions!=null) {
p = mMap.addPolyline(lineOptions);
polylines.add(p);

}

在主函数中:

for (int required=0;required<Points.size();required++) {
if(Points.get(required)!=FINAL.get(required)){
polylines.get(required).setVisible(false);
polylines.set(required,null);
}
drawPolylines(Points.get(required));
}

其中 FINAL 是旧位置,Points 是新位置数组。

它总是会导致:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.model.Polyline.setVisible(boolean)' on a null object reference

另一个尝试是remove()选项。

for(int ccc=0;ccc<polylines.size();ccc++) {
Polyline theta=polylines.get(ccc); //removing all the polylines in the array
theta.remove();
}
polylines.clear();
for (required=0;required<Points.size();required++) {
if(Points.get(required)!=FINAL.get(required))
drawPolylines(Points.get(required)); //drawing polylines again
}

但这并没有清除任何折线,而是重新绘制了现有的折线。

最佳答案

您可以在折线对象上调用remove(),如下所示

假设你添加了这样的折线

Polyline polyline = this.mMap.addPolyline(new PolylineOptions().....);

仅删除此折线

polyline.remove();

//重画的原因可能是

for (int required=0;required<Points.size();required++) {
if(Points.get(required)!=FINAL.get(required)){
polylines.get(required).setVisible(false);
polylines.set(required,null);
}
drawPolylines(Points.get(required)); // you are redrawing polyline here i guess
}

关于java - 折线未清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57305682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com