akexorcist / GoogleDirectionLibrary

[Android] Library for Google Direction API for Google Maps Android API v2
Apache License 2.0
571 stars 174 forks source link

Removing drawn direction from map #48

Closed eeffoc closed 6 years ago

eeffoc commented 6 years ago

Is it possible to remove a (simple) direction after adding it, without calling googleMap.clear(); ? Thanks

if (direction.isOK()){
    Route route = direction.getRouteList().get(0);
    ArrayList<LatLng> directionPositionList = route.getLegList().get(0).getDirectionPoint();
    mPolylineDirections = DirectionConverter.createPolyline(getApplicationContext(), directionPositionList, 5, getResources().getColor(R.color.colorSecondaryDark));
    mGoogleMap.addPolyline(mPolylineDirections);
}
starr0stealer commented 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();
blackisthesoul commented 6 years ago

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

eeffoc commented 6 years ago

Thank you @starr0stealer wasn't aware that an instance is returned!