首页 文章

Google Maps Android API v2,如何从 Map 中删除折线?

提问于
浏览
12

我正在尝试删除以前添加的折线,并在位置更改后重新绘制新的折线 . 我试过了两个

this.routeToDestination.setPoints(pointsToDestination) and this.routeToDestination.remove()

但他们都没有工作 .

我跟着How to draw a dynamic line (route) with Google Maps Android API v2但无法解决问题

@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);
    }

}

1 回答

  • 32

    要删除折线,您只需使用API中所述的remove()方法即可 .

    //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();
    

相关问题