user2523388
user2523388

Reputation: 112

Adding multiple points in map using polyline and arraylist

How to draw a polyline in google map for multiple latitude and longitude coordinates. I need to draw polyline for atleast 20 sets of latitude and longitude dynamically.

Upvotes: 2

Views: 15372

Answers (4)

olosunde ayooluwa
olosunde ayooluwa

Reputation: 9

You can iterate over the array of points and for each of the coordinates add a new LatLng to polyline options then after the loop add the polyline option to the polyline.

 val from = LatLng(it.data[0].lat, it.data[0].lng)
                            val to = LatLng(it.data.last().lat, it.data.last().lng)
                            map.addMarker(MarkerOptions().position(from).title("pickup location"))
                            map.addMarker(MarkerOptions().position(to).title("destination location"))
                            map.animateCamera(CameraUpdateFactory.newLatLngZoom(from, 13f)) 
                            var polylineOptions = PolylineOptions()
                            for (i in it.data){
                                polylineOptions.add(LatLng(i.lat, i.lng))
                            }

                            polylineOptions.width(5f).color(Color.RED)
                            map.addPolyline(
                                polylineOptions
                            )

Upvotes: 0

user3439968
user3439968

Reputation: 3538

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);

Upvotes: 7

Neonigma
Neonigma

Reputation: 1835

Just create a function to retrieve JSON polylines as this:

private ArrayList<LatLng> getPolylines(String jsonStr) {
    // file exists, it is the first boot
    if (jsonStr != null) {
        // linea init
        LatLng polyline;

        // array list of lines init
        ArrayList<LatLng> polylines = new ArrayList<LatLng>();

        // get json array
        JSONArray jsonArray = JSON.getJSONArray(jsonStr, "polytag");

        for (int i = 0; i < jsonArray.length(); i++) {

            JSONObject jsonPolyline;

            try {
                jsonPolyline = jsonArray.getJSONObject(i);

                polyline = new LatLng(Double.valueOf(jsonPolyline.getString("lat")),
                        Double.valueOf(jsonPolyline.getString("lon")));

                polylines.add(polyline);

            } catch (JSONException e) {
                Log.d(TAG, "JSONException reading polylines: " + e.getMessage());
            } catch (Exception e) {
                Log.d(TAG, "Exception reading polylines: " + e.getMessage());
            }
        }

        return polylines;
    }

    return null;
}

Then get this ArrayList and populate it in this way:

ArrayList<LatLng> polylines = getPolylines(linesJsonStr);
map.addPolyline(new PolylineOptions().addAll(polylines).width(2.0f).color(Color.RED));

Hope it helps!

Upvotes: 2

kriomant
kriomant

Reputation: 2326

Example from Android documentation:

   GoogleMap map;
   // ... get a map.
   // Add a thin red line from London to New York.
   Polyline line = map.addPolyline(new PolylineOptions()
     .add(new LatLng(51.5, -0.1), new LatLng(40.7, -74.0))
     .width(5)
     .color(Color.RED));

Just call .add as many times as you need.

Upvotes: 5

Related Questions