Google Maps Android API v2, how to remove Polylines from the map?

£可爱£侵袭症+ 提交于 2019-12-08 14:46:50

问题


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

}

回答1:


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


来源:https://stackoverflow.com/questions/15139271/google-maps-android-api-v2-how-to-remove-polylines-from-the-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!