How to dynamically add polylines from an arraylist
Asked Answered
S

3

5

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());
    }
Shortwinded answered 1/5, 2013 at 0:32 Comment(0)
G
8

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

Gliwice answered 1/5, 2013 at 12:30 Comment(3)
@Gliwice Hey I need some help for thisI am bit confuse with your above answer.pls help meExtravascular
@chintankhetiya what is the confusion?Gliwice
chat.stackoverflow.com/rooms/19132/java-and-android-era can we talk here if you don't mind so i can explain youExtravascular
S
17

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);
Supply answered 22/9, 2014 at 16:19 Comment(0)
G
8

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

Gliwice answered 1/5, 2013 at 12:30 Comment(3)
@Gliwice Hey I need some help for thisI am bit confuse with your above answer.pls help meExtravascular
@chintankhetiya what is the confusion?Gliwice
chat.stackoverflow.com/rooms/19132/java-and-android-era can we talk here if you don't mind so i can explain youExtravascular
C
0

What is the places variable you have because places need to be all the locations in the line and not just 1 point.

So assuming places is ArrayList<LatLng> then by doing places.get(i) you are only giving one point and not the whole list of points;

Clomp answered 1/5, 2013 at 1:48 Comment(3)
places is ArrayList<ArrayList<LatLng>> so get(i) is the first arraylist of LatLng'sShortwinded
are you sure there is actually something in the list then?Clomp
It draws one route and only one route. I have changed my code to this Polyline p1 = mMap.addPolyline(new PolylineOptions().addAll(places.get(0)).geodesic(true)); p1.setColor(Color.RED); Polyline p2 = mMap.addPolyline(new PolylineOptions().addAll(places.get(1)).geodesic(true)); p2.setColor(Color.LTGRAY); Polyline p3 = mMap.addPolyline(new PolylineOptions().addAll(places.get(2)).geodesic(true)); p3.setColor(Color.YELLOW); Polyline p4 = mMap.addPolyline(new PolylineOptions().addAll(places.get(3)).geodesic(true)); p4.setColor(Color.GREEN); and draws the green oneShortwinded

© 2022 - 2024 — McMap. All rights reserved.