android - Google Maps Android API v2,如何从 map 中删除折线?

标签 android google-maps-android-api-2

我正在尝试删除之前添加的多段线,并在位置更改后重新绘制新的多段线。我都试过了

this.routeToDestination.setPoints(pointsToDestination) 和 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);
    }

}

最佳答案

要删除折线,您只需使用 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();

关于android - Google Maps Android API v2,如何从 map 中删除折线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15139271/

相关文章:

java - 如何检查按钮是否在按钮数组中?

android - 如何在Google map 中标记纬度、经度?

android - getView() (对于自定义 ListView )不会在 notifyDatasetChanged() 上被调用

android - 按下时更改 ImageView 的源图像

android - 无法加载 AppCompat ActionBar,出现未知错误。Android Studio 3.1.3

android - 谷歌地图 android api v2 错误

Android Drawer Example,如何实现Google Map Fragment

android - SupportMapFragment 具有自定义布局,可能吗?

android - 固定 GPS 时获取坐标 - Android

java - Android-decodeBase64 导致应用程序崩溃