Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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());
    }
like image 855
aldito2 Avatar asked May 01 '13 00:05

aldito2


2 Answers

Adding multiple points in map using polyline and arraylist

ArrayList<LatLng> coordList = new ArrayList<LatLng>();

// Adding points to ArrayList
coordList.add(new LatLng(0, 0);
coordList.add(new LatLng(1, 1);
coordList.add(new LatLng(2, 2);
// etc...

// Find map fragment. This line work only with support library
GoogleMap gMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

PolylineOptions polylineOptions = new PolylineOptions();

// Create polyline options with existing LatLng ArrayList
polylineOptions.addAll(coordList);
polylineOptions
 .width(5)
 .color(Color.RED);

// Adding multiple points in map using polyline and arraylist
gMap.addPolyline(polylineOptions);
like image 54
user3439968 Avatar answered Sep 22 '22 10:09

user3439968


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

like image 39
Raghunandan Avatar answered Sep 19 '22 10:09

Raghunandan