首页 > 解决方案 > 如何在移动时删除从源到当前位置的折线路径

问题描述

我想在移动时删除以前的折线,我正在使用 GoogleDirection 类来绘制折线。请参考下面的代码片段

   GoogleDirection.withServerKey(getResources().getString(R.string.google_maps_key))
                .from(origin)
                .to(destination)
                .transportMode(TransportMode.DRIVING)
                .alternativeRoute(true)
                .execute(this);

 @Override
public void onDirectionSuccess(Direction direction, String rawBody) {   
            mMap.clear();
            mMap.addMarker(new MarkerOptions().position(destination));
            ArrayList<LatLng> directionPositionList = direction.getRouteList().get(0).getLegList().get(0).getDirectionPoint();
            polyline = mMap.addPolyline(DirectionConverter.createPolyline(this, directionPositionList, 3, Color.BLACK));

}
  @Override
public void onDirectionFailure(Throwable t) {
    Log.e("Direction fail Sorry", " no route found for this location.");

在此处输入图像描述 }

请参考截图,需要根据当前位置更新折线。

标签: androidgoogle-maps

解决方案


要删除折线,您应该简单地使用 remove() 方法

//Add line to map
Polyline confirmPathPlotLine= 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
confirmPathPlotLine.remove();

推荐阅读