Reputation: 67
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?
Upvotes: 0
Views: 726
Reputation: 11
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();
}
}
Upvotes: 1
Reputation: 206
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();
...
}
Upvotes: 0