Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps Android API v2, how to remove Polylines from the map?

I am trying to remove previously added Polyline and redraw new Polyline when the location has been changed. I tried both

this.routeToDestination.setPoints(pointsToDestination) and this.routeToDestination.remove()

but neither of them worked.

I followed How to draw a dynamic line (route) with Google Maps Android API v2 but could not resolved the issue

    @Override
    public void onResume() {
        super.onResume();

        routeToDestination = mMap.addPolyline(new PolylineOptions()
                .add(new LatLng(location.getLatitude(), location.getLongitude()),
                        new LatLng(this.destinationLatitude, this.destinationLongitude))
                .width(1)
                .color(Color.DKGRAY)

        );
    }

   @Override
    public void onLocationChanged(Location location) {

        List<LatLng> pointsToDestination = new ArrayList<LatLng>();
        pointsToDestination.add(new LatLng(location.getLatitude(), location.getLongitude()));
        pointsToDestination.add(new LatLng(destinationLatitude, destinationLongitude));

        this.routeToDestination.setPoints(pointsToDestination);
    }

}
like image 893
DeadManSpirit Avatar asked Feb 28 '13 15:02

DeadManSpirit


1 Answers

To remove a polyline you should simply use remove() method as stated in the API.

//Add line to map
Polyline line = 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();
like image 182
Rawa Avatar answered Nov 09 '22 10:11

Rawa