Polylines not clearing

Polylines are not clearing inspite of using setVisibility(false)

We created a arraylist of polylines and added to it whenever a new polyline is added. But we want to clear all the polylines without clearing the map, and there is no success.
We tried to use the setPoints option using the polyline array, but we are not able to retrieve the polyline required from the arraylist. It always gives IndexOutOfBounds error.

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);

                }

In the main function:

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));
}

Where FINAL are the old locations and Points are the new locations array.

It always results in:

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

Another attempt was the remove() option.

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
      }

But this did not clear any polyline, but redrew over the existing ones.

Leave a Reply

avatar
  Subscribe  
Notify of