Question

I'm drawing polylines in google map v2 using marker drag, this is my code

int startDraw = 1;
Polyline polyline;
List<LatLng> lineCordinates = new ArrayList<LatLng>();
List<Polyline> polylines = new ArrayList<Polyline>();

@Override
public void onMarkerDrag(Marker marker) {
    // startDraw == 0 --> stop draw
    // startDraw == 1 --> start draw

if (startDraw == 1) {

    Integer markerId = markerMapHash.get(marker);

    if (markerId == null) {
        markerId = 1;
    }

    if (markerId == 1) {
        lineCordinates.add(marker.getPosition());

    }


    for (int i = 0; i < lineCordinates.size(); i++) {

        polyline = myMap.addPolyline(new PolylineOptions()
        .addAll(lineCordinates)
        .width(lineWidth)
        .color(lineColor));

        polylines.add(polyline);

    }
    }

   }


@Override
public void onMarkerDragEnd(Marker marker) {
    lineCordinates.clear(); 
    if (startDraw == 1) {
        startDraw = 0;
    }else{
        startDraw = 1;
    }

   }

and to remove all polylines, this is the code in a button onClick

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

Now I want to remove a specific polyline, how can I do this ? Hope anyone could help me. Thanks in advance.

Was it helpful?

Solution

I've never messed with this code before, but I assume you just need to check to see if line matches the specific polyline you're looking for and then remove it. Something like this:

for(Polyline line : polylines)
{
    if (isSpecificPolyline(line))
        line.remove();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top