Search code examples
androidgoogle-maps-api-2polylinegoogle-polyline

Remove polyline not working outside a function


I am trying to remove polylines in my google map. I have defined a function that adds polylines between two points as follows:

    public void addPolylineBetweenTwoPoint(LatLng point1, LatLng point2, int color) {
        PolylineOptions path = new PolylineOptions()
                .color(color)
                .zIndex(10); // Closes the polyline.

        path.add(point1, point2);

        polyline = map.addPolyline(path);
        // polyline.remove();
    }

Now, in my program, I am doing as follows:

    addPolylineBetweenTwoPoint(startingPointCoordinates, nearestStartingPoint.coordinates, Color.BLUE);
    polyline.remove();

The problem is that when I am calling polyline.remove() after my function, it is not working and does not remove the polyline. But if I uncomment polyline.remove(); statement in my function, then the polyline is removing correctly. Since my polyline object is same, therefore the polyline should be removed in both cases. But I do not know what is the problem.

Moreover, I already have declared polyline and map objects in my class as follows:

    public Polyline polyline;
    private GoogleMap map;

Solution

  • The procedure for removing specific polyline is described here:

    Removing a polyline from google maps