我试图删除previously增加折线,当位置已经改变重绘新的折线。我想这两个
this.routeToDestination.setPoints(pointsToDestination)和 this.routeToDestination.remove()
但他们都没有工作。
我也跟着How绘制与谷歌地图Android版API V2 动态线(路线),但未能解决该问题。
@覆盖 公共无效onResume(){ super.onResume(); routeToDestination = mMap.addPolyline(新的PolylineOptions() 。新增(新经纬度(location.getLatitude(),location.getLongitude()), 新的经纬度(this.destinationLatitude,this.destinationLongitude)) .WIDTH(1) 。颜色(Color.DKGRAY) ); } @覆盖 公共无效onLocationChanged(地点){ 清单<&经纬度GT; pointsToDestination =新的ArrayList<&经纬度GT;(); pointsToDestination.add(新经纬度(location.getLatitude(),location.getLongitude())); pointsToDestination.add(新经纬度(destinationLatitude,destinationLongitude)); this.routeToDestination.setPoints(pointsToDestination); }}
解决方案
要删除您只需要使用remove()方法的API中所述的折线。
//添加行地图折线行= mMap.addPolyline(新的PolylineOptions() 。新增(新经纬度(location.getLatitude(),location.getLongitude()), 新的经纬度(this.destinationLatitude,this.destinationLongitude)) .WIDTH(1) 。颜色(Color.DKGRAY)//删除从地图中的同一行line.remove();
I am trying to remove previously added Polyline and redraw new Polyline when the location has been changed. I tried both
this.routeToDestination.setPoints(pointsToDestination) and this.routeToDestination.remove()
but neither of them worked.
I followed How to draw a dynamic line (route) with Google Maps Android API v2 but could not resolved the issue
@Override
public void onResume() {
super.onResume();
routeToDestination = mMap.addPolyline(new PolylineOptions()
.add(new LatLng(location.getLatitude(), location.getLongitude()),
new LatLng(this.destinationLatitude, this.destinationLongitude))
.width(1)
.color(Color.DKGRAY)
);
}
@Override
public void onLocationChanged(Location location) {
List<LatLng> pointsToDestination = new ArrayList<LatLng>();
pointsToDestination.add(new LatLng(location.getLatitude(), location.getLongitude()));
pointsToDestination.add(new LatLng(destinationLatitude, destinationLongitude));
this.routeToDestination.setPoints(pointsToDestination);
}
}
解决方案
To remove a polyline you should simply use remove() method as stated in the API.
//Add line to map
Polyline line = mMap.addPolyline(new PolylineOptions()
.add(new LatLng(location.getLatitude(), location.getLongitude()),
new LatLng(this.destinationLatitude, this.destinationLongitude))
.width(1)
.color(Color.DKGRAY)
//Remove the same line from map
line.remove();