如何使用PolylineOptions从Google地图安卓版V2中清除路线并重新绘制一条新的路线?

3

我正在获取每次onLocationChange的纬度和经度,并根据这些信息制作路线,如下所示:

public void onLocationChanged(Location location) {


      PolylineOptions rectLine2;
      rectLine2.add(new LatLng(lat, lon));
      mGoogleMap.addPolyline(rectLine2);
}

现在我希望可以一键清除这条路线,再次移动时,应该重新绘制新的路线,但是在我的情况下,旧路线没有被清除。
我已经尝试了以下方法,但并没有起作用:
mGoogleMap.clear();// It clear's the map, but when I again draw the old route again comes.

此外
Polyline polyline;
polyline = mGoogleMap.addPolyline(rectLine2);
polyline.reomve()// This also didn't work.

还有其他方法可以清除地图吗?

谢谢提前!


尝试使用mGoogleMap.getPolylines().clear()。 - Haresh Chhelana
@HareshChhelana,GoogleMap中没有这样的方法。 - B-GangsteR
5个回答

7

mGoogleMap.clear()

这个函数用来清空整个地图,以便重新绘制多段线路...

否则,您需要将多段线路分配给一个变量,然后将其删除...

PolylineOptions rectLine2;
rectLine2.add(new LatLng(lat, lon));
Polyline polyline = mGoogleMap.addPolyline(rectLine2);
polyline.remove();

以下是清除多条折线的方法,如果你需要清除折线,只需创建一个数组,并在需要清除时删除所有折线。

这些是方法... 为什么还需要其他解决方案来清除呢?


0
使用map.clear()方法清除所有地图(折线、标记等),或将所有折线保存在某个地方,然后删除它们以保存其他所有内容。
                    for(Polyline polyline : polylineList) {
                    polyline.remove();
                }

0

嗯,有趣,试试这个方法,对我有效:

private void clearPath() {
    if (polyline != null)
        polyline.remove();
    polyline = null;
}

0

尝试清除您的rectLine2变量。可能它仍然保留着旧的latlangs。因此在您的点击事件中进行清除。

rectLine2 = new PolylineOptions();

是的,你说得对,但它没有清除方法。 这意味着PolylineOptions没有清除方法。 - Dheerendra Upadhyay

0

mGoogleMap.clear() 是清除地图上所有标记、折线、多边形、覆盖物等的最佳方法。请参见 http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html#clear()

所以问题不在于此。您在重新绘制新路径时可能出了些问题。我认为您没有在 onLocationChanged 函数中提供所有代码。如果这是您的最终代码,那么它应该无法正常工作,因为您只添加了一个点到 PolylineOptions 中。 请查看 How to draw interactive Polyline on route google maps v2 android

希望能对您有所帮助。


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