0

I'm trying to remove my previously created polyline before creating one, this is what I have:

    protected void fazerCaminho(ArrayList<HashMap<String, String>> listaPolylines) {
    if (line == null) {
        for (Map polyline : listaPolylines) {
            List<LatLng> decodedPath = PolyUtil.decode((String) polyline.get("points"));
            line = mMap.addPolyline(new PolylineOptions()
                    .width(3)
                    .color(Color.rgb(25, 151, 152))
                    .geodesic(true)
                    .addAll(decodedPath));
        }

    } else {
        line.remove();
        for (Map polyline : listaPolylines) {
            List<LatLng> decodedPath = PolyUtil.decode((String) polyline.get("points"));
            line = mMap.addPolyline(new PolylineOptions()
                    .width(3)
                    .color(Color.rgb(25, 151, 152))
                    .geodesic(true)
                    .addAll(decodedPath));
        }
    }
}

The line.remove doesn't work, this solution worked on a previous version but back then I didn't have the for method, what am I doing wrong?

alb
  • 347
  • 2
  • 7
  • 24
  • You are removing the last line only – Nizam Apr 04 '17 at 11:19
  • 1
    check out this http://stackoverflow.com/questions/14853084/how-to-remove-all-the-polylines-from-a-map – Abid Khan Apr 04 '17 at 11:20
  • Possible duplicate of [How to remove all the polylines from a map](http://stackoverflow.com/questions/14853084/how-to-remove-all-the-polylines-from-a-map) – Nizam Apr 04 '17 at 11:22

1 Answers1

0

Create a List to store your Polylines and clear them before adding new ones :

private List<Polyline> lines = new ArrayList<>();

// ...

private void removeLines() {
    for (Polyline line : lines) {
        line.remove();
    }
    lines.clear();
}

// ...

protected void fazerCaminho(ArrayList<HashMap<String, String>> listaPolylines) {
    removeLines();

    for (Map polyline : listaPolylines) {
        List<LatLng> decodedPath = PolyUtil.decode((String) polyline.get("points"));
        lines.add(mMap.addPolyline(new PolylineOptions()
                .width(3)
                .color(Color.rgb(25, 151, 152))
                .geodesic(true)
                .addAll(decodedPath)));
    }
}
antonio
  • 18,044
  • 4
  • 45
  • 61