【问题标题】:Polylines not clearing折线未清除
【发布时间】:2019-12-09 20:29:46
【问题描述】:

尽管使用了 setVisibility(false),但折线不会清除

我们创建了一个折线数组列表,并在添加新折线时添加到其中。但是我们想在不清除地图的情况下清除所有折线,并没有成功。 我们尝试通过折线数组使用 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
      }

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

【问题讨论】:

    标签: java android api maps polyline


    【解决方案1】:

    您可以在折线对象上调用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 
    }
    

    【讨论】:

    • 是的,但是我们有多个折线。我们尝试了一个 for 循环,对所有这些都调用了 remove 选项,但还是徒劳无功,它仍在地图上重绘。 for(int ccc=0;ccc
    • 即使你使用for循环也没关系
    • 在多段线上调用 remove() 之前有没有设置条件
    • 我应该考虑哪些代码有问题或发表在评论中?
    • 我们正在使用一个条件来检查 FINAL 是否为空,我认为这不会影响,因为该块下的所有其他东西都在工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 2020-06-09
    • 2022-10-30
    • 1970-01-01
    • 2011-10-02
    相关资源
    最近更新 更多