2016-11-30 70 views
1

我在地图上绘制了一个像动画一样的polyline。如下所示。Android从地图v2中移除折线

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();但它不工作。

+0

您确定没有多次添加该折线吗?所以当你试图删除它时,下面还有一个让你觉得它从未被删除的东西? – Jaythaking

+0

我试图将它们存储在一个ArrayList中,每次创建一个,然后当需要删除时,遍历该数组并删除它们... – Jaythaking

+0

@jaythakin你可以给我一个添加和删除的例子。 – user3555472

回答

3

你只是做以下但不是结果赋值给一个变量,把它放在一个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(); 

从地图中删除此折线。在多段线已被移除后,其所有方法的行为都是不确定的。

+0

感谢您的快速回复。我不知道如何在arraylist中添加多段线并在地图上显示:(。我需要那个解决方案:( – user3555472

+0

我编辑了答案... – Jaythaking

+0

非常感谢你.......我明天会试试并且会让你知道。再次感谢你的人 – user3555472