Tamas Koos
Tamas Koos

Reputation: 1063

Polyline add points with for loop

I have coordinates in arrays.xml like this:

<string-array name="b26">
    <item>47.183845335746227,18.408230864565329</item>
    <item>47.1835746,18.4079741</item>
    .
    .
    .
    <item>47.181805616004361,18.408938496194111</item>
</string-array>

I read it into an array, then split it at the "," and put the lat and lon coordinates into two double arraylists. Then I would like to add those points to PolylineOptions so that I can draw a polyline. But the part I marked doesn't work. How should I do it?

private GoogleMap mMap;
String[] array;
String[] separated;
ArrayList<Double> lat = new ArrayList<Double>();
ArrayList<Double> lon = new ArrayList<Double>();

array = getResources().getStringArray(R.array.b26);
    for(int i=0; i<array.length; i++){
        separated = array[i].split(",");
        lat.add(Double.parseDouble(separated[0]));
        lon.add(Double.parseDouble(separated[1]));}
    PolylineOptions rectOptions = new PolylineOptions()
            for(int i=0; i<array.length; i++){
                .add(new LatLng(lat.get(i), lon.get(i)));}  <===================

    rectOptions.color(Color.RED);
    mMap.addPolyline(rectOptions);

Upvotes: 0

Views: 1410

Answers (1)

antonio
antonio

Reputation: 18252

Just create a List<LatLng> with your LatLngs and add them to the PolylineOptions using the addAll method:

List<LatLng> latlngs = new ArrayList<>();
for(int i=0, i<array.length, i++){
    latlngs.add(new LatLng(lat.get(i), lon.get(i));)
}

PolylineOptions rectOptions = new PolylineOptions().addAll(latlngs);

Upvotes: 2

Related Questions