[英]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. 我正在使用jd-alexander图书馆在Google地图上显示路线,而我的问题是,当我请求其他路线时,会创建新路线,但不会删除以前创建的路线。 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();
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.