简体   繁体   中英

Remove previous direction route on Google Maps

I'm using jd-alexander liabrary to show direction route on google map and my issue is, when I request another direction route, new route is created but previously created route is not removed. I want to remove the previous route and then show new route.

@Override
public void onRoutingSuccess(ArrayList<Route> routeArrayList, int i) {

    //code to add route to map here.
     polylines = new ArrayList<>();
     if (polylines.size() > 0) {
         erasePolylines();
     }
         polylines = new ArrayList<>();
         //add route(s) to the map.
     polylines.clear();
         for (i = 0; i < routeArrayList.size(); i++) {

             //In case of more than 5 alternative routes
             int colorIndex = i % COLORS.length;

             PolylineOptions polyOptions = new PolylineOptions();
             polyOptions.color(getResources().getColor(COLORS[colorIndex]));
             polyOptions.width(10 + i * 3);
             polyOptions.addAll(routeArrayList.get(i).getPoints());
             Polyline polyline = mMap.addPolyline(polyOptions);
             polylines.add(polyline);

             Toast.makeText(getApplicationContext(), "Route " + (i + 1) + ": distance - " + routeArrayList.get(i).
                     getDistanceValue() + ": duration - " + routeArrayList.get(i).getDurationValue(), Toast.LENGTH_SHORT).show();
         }
 }

private void erasePolylines(){
    for(Polyline line : polylines){
        line.remove();
    }
    polylines.clear();
}

How can I fix this?

polylines = new ArrayList<>();
 if (polylines.size() > 0) {
     for (Polyline poly : polylines) {
         poly.remove();
     }
     erasePolylines();
 }
     polylines = new ArrayList<>();
     //add route(s) to the map.
 polylines.clear();
     for (i = 0; i < routeArrayList.size(); i++) {

         //In case of more than 5 alternative routes
         int colorIndex = i % COLORS.length;

         PolylineOptions polyOptions = new PolylineOptions();
         polyOptions.color(getResources().getColor(COLORS[colorIndex]));
         polyOptions.width(10 + i * 3);
         polyOptions.addAll(routeArrayList.get(i).getPoints());
         Polyline polyline = mMap.addPolyline(polyOptions);
         polylines.add(polyline);

         Toast.makeText(getApplicationContext(), "Route " + (i + 1) + ": distance - " + routeArrayList.get(i).
                 getDistanceValue() + ": duration - " + routeArrayList.get(i).getDurationValue(), Toast.LENGTH_SHORT).show();
     }
 }

Your code is ok, except here:

//You're initializing your list "POLILYNES" and trying remove empty list, Remove the 
//lines I commented.

@Override
public void onRoutingSuccess(ArrayList<Route> routeArrayList, int i) {

//code to add route to map here.
 polylines = new ArrayList<>(); // DELETE THIS LINE
 if (polylines.size() > 0) {
     erasePolylines();
 }
     polylines = new ArrayList<>(); // DELETE THIS LINE
     //add route(s) to the map.
 polylines.clear();

    ...
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM