Search code examples
androidgoogle-mapsgoogle-polyline

Dynamically draw a single polyline to current location


In my android app, I want to create a single path/polyline from a specific location(far end) to my current location while moving. I could draw the polyline on map with now issue while when I moves to a new location another polyline is been drown on the map, so my map output is full of polylines whichh I don't need, I want only one polyline to be visible to my current location.

            PolylineOptions polylineOptions = new PolylineOptions().add(currentPosition).add(farEndPosition).width(10).color(Color.GREEN);
            Polyline polyline = googleMap.addPolyline(polylineOptions);

Solution

  • Remove old Line before adding new one.

       Polyline  polyline ;
    
            public void addUpdatePolyLine()
                {
                 PolylineOptions polylineOptions = new PolylineOptions().add(currentPosition).add(farEndPosition).width(10).color(Color.GREEN);
                  if(polyline !=null)
                  {
                  polyline.remove();
                    }
                polyline = googleMap.addPolyline(polylineOptions);
                }