谷歌地图V2 Android中删除折线的问题

10
我正在按照谷歌开发者网站的方式添加折线选项。
PolylineOptions rectOptions = new PolylineOptions()
        .add(new LatLng(37.35, -122.0))
        .add(new LatLng(37.45, -122.0))  // North of the previous point, but at the same longitude
        .add(new LatLng(37.45, -122.2))  // Same latitude, and 30km to the west
        .add(new LatLng(37.35, -122.2))  // Same longitude, and 16km to the south
        .add(new LatLng(37.35, -122.0)); // Closes the polyline.

// Get back the mutable Polyline
Polyline polyline = myMap.addPolyline(rectOptions);

我想要删除它,但是没有rectOptions.remove()。如Google Maps Android API v2, how to remove Polylines from the map?所述,我从SDK更新了google play服务,但仍然没有它。在只更新SDK管理器之后,我应该做些什么吗?我真的需要将其移除而不是使其不可见以节省内存,因为我将显示很多点的路径多次。
2个回答

11

要删除Polyline,请使用polyline.remove();


所以我猜当你使用PolylineOptions x;声明时,你不能使用remove()。remove仅适用于Polyline对象.. 所以我使用了这个。 - Vivere_FlowCoder
PolylineOptions只是为了使折线的初始化更容易(其他*Options类也是如此)。您需要在折线本身上进行每个折线的操作。 - Mokus
@Mokus,我有一个多边形,正在执行与您发布答案相同的操作,但这不会从地图中删除该多边形。我该如何刷新它?请参见文档 - Chintan Khetiya
还需要 map.clear();。但是要小心地处理地图上的其他事情! :) - MHSaffari

2

不应该使用 PolylineOptions 来删除它。

可以使用如下方式来使用 PolyLine 实现删除

polyline.remove();

文档

public void remove ()

从地图中移除此折线。在折线被移除后,所有其方法的行为都是未定义的。


是的,我明白了。我改成了折线。谢谢Mitesh。 - Vivere_FlowCoder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接