Search code examples
androidgoogle-mapsdynamically-generatedgoogle-polyline

How to dynamically add polylines from an arraylist


I have an

places = ArrayList<ArrayList<LatLng>>

I am adding LatLng points into the inner ArrayList and then I have a for loop that loops and adds polylines to the map.. except it doesnt do that... How can I add polylines dynamically to the GoogleMap? I checked whether or not places was being populated and it is.

Thanks in advance.

ArrayList<Polyline> pl = new ArrayList<Polyline>();                 
for(int i =0; i<places.size(); i++){
        pl.add(mMap.addPolyline(new PolylineOptions().addAll(places.get(i))));
        Log.e("size of places", "size of places is " + places.size());
    }

Solution

  • Once you have list of latitude an longitudes in your List, you can use the below to draw lines.

    List<LatLng> points = decodePoly(_path); // list of latlng
    for (int i = 0; i < points.size() - 1; i++) {
      LatLng src = points.get(i);
      LatLng dest = points.get(i + 1);
    
      // mMap is the Map Object
      Polyline line = mMap.addPolyline(
        new PolylineOptions().add(
          new LatLng(src.latitude, src.longitude),
          new LatLng(dest.latitude,dest.longitude)
        ).width(2).color(Color.BLUE).geodesic(true)
      );
    }
    

    the above worked for me in my application