Reputation: 381
I'm drawing lines between two position on map and it works fine .
I need to remove them when I want to draw new line . This is my code :
PolylineOptions lineOptions;
ArrayList<LatLng> points = null;
for(int i=0;i<result.size();i++){
points = new ArrayList<LatLng>();
List<HashMap<String, String>> path = result.get(i);
for(int j=0;j<path.size();j++){
HashMap<String,String> point = path.get(j);
if(j==0){
distance = (String)point.get("distance");
continue;
}else if(j==1){
duration = (String)point.get("duration");
continue;
}
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
}
lineOptions.addAll(points);
lineOptions.width(3);
lineOptions.color(Color.RED);
map.addPolyline(lineOptions);
I tried map.clear() , didn't work . How can I clear all lines on the map ?
thanks you
Upvotes: 0
Views: 133
Reputation: 810
Make an ArrayList
ArrayList<Polyline> polylineArraylist;
In onCreate Method
polylineArraylist = new ArrayList<Polyline>();
Update your code with this one
// flusing all Markers drawn previously
for (int icount = 0; icount < polylineArraylist.size(); icount++) {
Polyline polyline = polylineArraylist.get(icount);
polyline.remove();
}
Polyline mPolyline = null;
PolylineOptions lineOptions;
ArrayList<LatLng> points = null;
for(int i=0;i<result.size();i++){
points = new ArrayList<LatLng>();
List<HashMap<String, String>> path = result.get(i);
for(int j=0;j<path.size();j++){
HashMap<String,String> point = path.get(j);
if(j==0){
distance = (String)point.get("distance");
continue;
}else if(j==1){
duration = (String)point.get("duration");
continue;
}
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
}
lineOptions.addAll(points);
lineOptions.width(3);
lineOptions.color(Color.RED);
mPolyline = googleMap.addPolyline(lineOptions);
Add Polyline to ArrayList
polylineArraylist.add(mPolyline);
Upvotes: 3