How to remove all the polylines from a map

[亡魂溺海] 提交于 2019-11-30 03:04:11

Keep track of the Polyline as you add it to the map:

Polyline polyline = this.mMap.addPolyline(new PolylineOptions().....);

Then when you want to remove it:

polyline.remove();

If you have lots of Polylines, just add them to a List as they are put on the map:

List<Polyline> polylines = new ArrayList<Polyline>();

for(....)
{
    polylines.add(this.mMap.addPolyline(new PolylineOptions()....));
}

And when you want to delete:

for(Polyline line : polylines)
{
    line.remove();
}

polylines.clear();

The key is to keep a reference to the Polyline objects and call .remove() on each one.

I know this is very old question but I noticed that this is very common need. I found another way and I wanted to share it.

Here is the basic idea:

Polyline polylineFinal;
PolylineOptions polylineOptions;

loop {

    polylineOptions.add( new LatLng( latitude, longitude ) );

}

polylineOptions.width(2);
polylineOptions.color(Color.RED);
polylineOptions.geodesic(true);

polylineFinal = map.addPolyline (polylineOptions);

Map's "addPolyline" method returns a single polyline which contains all the points. When I need to remove the points, I call polylineFinal's "remove" method.

polylineFinal.remove();

I used this work :)

    private GoogleMap mMapView;

    SupportMapFragment myMapFragment = (SupportMapFragment)getSupportFragmentManager()
    .findFragmentById(R.id.mMapView3);
    mMapView = myMapFragment.getMap();

    PolylineOptions rectLine = new PolylineOptions()
                    .add(new LatLng(13.000000, 100.000000))
                    .add(new LatLng(13.010000, 100.000000));

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