Reputation: 101
I want the polyline that is already created on another method to be removed. Is there a way to remove that specific polyline? Below is my codes for adding polyline.
public void lardizabalToTayuman() {
map.addPolyline(new PolylineOptions().add(
new LatLng(14.617071, 120.989945),
new LatLng(14.605693, 121.000863),
new LatLng(14.605599, 121.000939),
new LatLng(14.603097, 121.001786),
new LatLng(14.602900, 121.001089),
new LatLng(14.605246, 121.000252),
new LatLng(14.613429, 120.992410),
new LatLng(14.611934, 120.990768),
new LatLng(14.617094, 120.985795)
)
.width(5)
.color(Color.RED)
);
}
My onMapReady codes:
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
LatLng manila = new LatLng(14.5995, 120.9842);
MarkerOptions option = new MarkerOptions();
option.position(manila).title("Manila");
map.addMarker(option);
map.moveCamera(CameraUpdateFactory.newLatLng(manila));
lardizabalToTayuman();
}
Upvotes: 0
Views: 403
Reputation: 11622
You can get the reference of the Polyline
and just call .remove(), it will remove the polyline.
Exp :
Polyline myPolyline = map.addPolyline(new PolylineOptions().add(
new LatLng(14.617071, 120.989945),..
));
and while removing just call myPolyline.remove();
Note :
Added
Polyline myPolyline
as a global variable, So that you can access in other methods.
Edited :
In you case you can do like this,
private Polyline myPolyline;
public void lardizabalToTayuman() {
myPolyline = map.addPolyline(new PolylineOptions().add(
new LatLng(14.617071, 120.989945),
new LatLng(14.605693, 121.000863),
new LatLng(14.605599, 121.000939),
new LatLng(14.603097, 121.001786),
new LatLng(14.602900, 121.001089),
new LatLng(14.605246, 121.000252),
new LatLng(14.613429, 120.992410),
new LatLng(14.611934, 120.990768),
new LatLng(14.617094, 120.985795)
)
.width(5)
.color(Color.RED)
);
}
When you want to remove just call this method,
private void removeLardizabalToTayuman() {
if(myPolyline != null) {
myPolyline.remove();
}
}
Upvotes: 2