我在地图上的两个位置之间画线,效果很好。
当我想绘制新线时,我需要删除它们。这是我的代码:
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);
我尝试了map.clear(),没有工作。我怎样才能清除地图上的所有线条?
谢谢你
创建一个数组列表
ArrayList<Polyline> polylineArraylist;
In onCreate Method
polylineArraylist = new ArrayList<Polyline>();
用这个
更新你的代码// 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);
添加折线到数组列表
polylineArraylist.add(mPolyline);