Android 从地图 v2 中删除折线

Android remove polyline from map v2

我正在像动画一样在地图上画一个polyline。如下所示。

m_handler = new Handler();
        m_handlerTask = new Runnable() {
            @Override
            public void run() {
                //line.remove();
                if (t < pointsPoly.size() - 1) {

                    LatLng src = pointsPoly.get(t);
                    LatLng dest = pointsPoly.get(t + 1);
                    Polyline lineAnimation = googleMap.addPolyline(new PolylineOptions()
                            .add(new LatLng(src.latitude, src.longitude),
                                    new LatLng(dest.latitude, dest.longitude))
                            .width(10).color(Color.DKGRAY).geodesic(true));
                    t++;

                } else {
                    t = 0;

                }
                m_handler.postDelayed(m_handlerTask, polyLineTimer);
            }
        };
        m_handler.post(m_handlerTask);

如何删除 polyline?我不想clearMap()。 我尝试了 lineAnimation.remove(); 但它不起作用。

您只需执行以下操作,而不是将结果分配给变量,而是将其放入 ArrayList...

    ArrayList<Polyline> lines = new ArrayList<>();
    //Add line to map
    lines.add(mMap.addPolyline(new PolylineOptions()
                .add(new LatLng(location.getLatitude(), location.getLongitude()),
                        new LatLng(this.destinationLatitude, this.destinationLongitude))
                .width(1)
                .color(Color.DKGRAY));

    //Remove the same line from map
    line.remove();

Removes this polyline from the map. After a polyline has been removed, the behavior of all its methods is undefined.