How to remove all the polylines from a map

前端 未结 3 820
余生分开走
余生分开走 2020-12-25 10:10

Following

How to draw a path between two markers

I had to add lot of polylines between two markers, to make a path.

One of the markers is draggable,

相关标签:
3条回答
  • 2020-12-25 10:26

    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.

    0 讨论(0)
  • 2020-12-25 10:26

    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();
    
    0 讨论(0)
  • 2020-12-25 10:28

    Update

    If you want to removes all markers, polylines, polygons, overlays, etc from the map use

    mMap.clear(); //it will remove all additional objects from map 
    

    and if you only want to remove all or single polyline, polygon, marker etc see @DiskDev Answer above. In this case you must keep track of every single additional object you add to map

    0 讨论(0)
提交回复
热议问题