Closed eeffoc closed 6 years ago
@eeffoc
When you call googleMap.addPolyline()
it returns the instance reference. Assign it to a variable, and then you can use the methods within the Polyline
class. With the reference you can call Polyline.remove()
.
[Google Docs: Polyline.remove](https://developers.google.com/android/reference/com/google/android/gms/maps/model/Polyline.html#remove())
private Polyline directionsPath;
directionsPath = googleMap.addPolyline();
directionsPath.remove();
Just wanna share what I have done
`
//put List on declaration
List<Polyline> polylines = new ArrayList<Polyline>();
@Override
public void onDirectionSuccess(Direction direction, String rawBody) {
if (direction.isOK()) {
for (int i = 0; i < direction.getRouteList().size(); i++) {
Route route = direction.getRouteList().get(i);
mMap.addMarker(new MarkerOptions().position(origin));
mMap.addMarker(new MarkerOptions().position(destination));
ArrayList<LatLng> directionPositionList = route.getLegList().get(0).getDirectionPoint();
polylines.add(mMap.addPolyline(DirectionConverter.createPolyline(this, directionPositionList, 5, Color.RED)));
}
setCameraWithCoordinationBounds(direction.getRouteList().get(0));
//btnRequestDirection.setVisibility(View.GONE);
} else {
//Snackbar.make(btnRequestDirection, direction.getStatus(), Snackbar.LENGTH_SHORT).show();
}
}`
And then when I want to remove polylines, I create a button to clear all my polylines
public void Clear(View view){ for(Polyline line : polylines) { line.remove(); } polylines.clear(); }
I hope it helps
Thank you @starr0stealer wasn't aware that an instance is returned!
Is it possible to remove a (simple) direction after adding it, without calling
googleMap.clear();
? Thanks