title: How to draw route more than two points on Google map in android?
body: <p>I want to display multiple points on a Google map with a route. The points should be dynamic, with input given by the user.</p>
  
  <p>The output will look like as given below image :</p>
  
  <p><a href="https://i.stack.imgur.com/K9Z0O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/K9Z0O.png" alt="enter image description here"></a></p>
  
  <p>I want to show the driving route between multiple locations in my Android app.</p>
  
  <p>There are several answers on Stack Overflow itself, and all of them were using the same method. Get the directions from start point to destination using Google directions API, and draw a polyline across the points returned.</p>
  
  <p>I used the below code but it shows the route between only two points. However, I want to display the route between multiple points.</p>
  
  <p>Thanks in advance.</p>
  
  <pre><code>private GoogleMap mMap;
  ArrayList&lt;LatLng&gt; markerPoints;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_maps);
      markerPoints = new ArrayList&lt;LatLng&gt;();
  
      SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
      mapFragment.getMapAsync(this);
      Button btnDraw = (Button) findViewById(R.id.btn_draw);
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
          return;
      }
      mMap.setMyLocationEnabled(true);
  
      mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
          @Override
          public void onMapClick(LatLng point) {
              if (markerPoints.size() &gt;= 10) {
                  return;
              }
  
              markerPoints.add(point);
              MarkerOptions options = new MarkerOptions();
              options.position(point);
  
              if (markerPoints.size() == 1) {
                  options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
              } else if (markerPoints.size() == 2) {
                  options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
              } else {
                  options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
              }
              mMap.addMarker(options);
          }
      });
  
      mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
          @Override
          public void onMapLongClick(LatLng point) {
              mMap.clear();
              markerPoints.clear();
  
          }
      });
  
      btnDraw.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (markerPoints.size() &gt;= 2) {
                  LatLng origin = markerPoints.get(0);
                  LatLng dest = markerPoints.get(1);
                  String url = getDirectionsUrl(origin, dest);
                  DownloadTask downloadTask = new DownloadTask();
                  downloadTask.execute(url);
              }
          }
      });
  }
  
  private String getDirectionsUrl(LatLng origin, LatLng dest) {
      String str_origin = "origin=" + origin.latitude + "," + origin.longitude;
      String str_dest = "destination=" + dest.latitude + "," + dest.longitude;
      String sensor = "sensor=false";
      String waypoints = "";
      for (int i = 2; i &lt; markerPoints.size(); i++) {
          LatLng point = (LatLng) markerPoints.get(i);
          if (i == 2)
              waypoints = "waypoints=";
          waypoints += point.latitude + "," + point.longitude + "|";
      }
  
      String parameters = str_origin + "&amp;" + str_dest + "&amp;" + sensor + "&amp;" + waypoints;
  
      String output = "json";
      String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;
      return url;
  }
  
  @SuppressLint("LongLogTag")
  private String downloadUrl(String strUrl) throws IOException {
      String data = "";
      InputStream iStream = null;
      HttpURLConnection urlConnection = null;
      try {
          URL url = new URL(strUrl);
          urlConnection = (HttpURLConnection) url.openConnection();
          urlConnection.connect();
          iStream = urlConnection.getInputStream();
          BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
          StringBuffer sb = new StringBuffer();
  
          String line = "";
          while ((line = br.readLine()) != null) {
              sb.append(line);
          }
          data = sb.toString();
          br.close();
      } catch (Exception e) {
          Log.d("Exception while downloading url", e.toString());
      } finally {
          iStream.close();
          urlConnection.disconnect();
      }
      return data;
  }
  
  private class DownloadTask extends AsyncTask&lt;String, Void, String&gt; {
      @Override
      protected String doInBackground(String... url) {
          String data = "";
          try {
              data = downloadUrl(url[0]);
          } catch (Exception e) {
              Log.d("Background Task", e.toString());
          }
          return data;
      }
  
      @Override
      protected void onPostExecute(String result) {
          super.onPostExecute(result);
          ParserTask parserTask = new ParserTask();
  
          parserTask.execute(result);
      }
  }
  
  private class ParserTask extends AsyncTask&lt;String, Integer, List&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt;&gt; {
  
      @Override
      protected List&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt; doInBackground(String... jsonData) {
  
          JSONObject jObject;
          List&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt; routes = null;
  
          try {
              jObject = new JSONObject(jsonData[0]);
              DirectionsJSONParser parser = new DirectionsJSONParser();
  
              routes = parser.parse(jObject);
          } catch (Exception e) {
              e.printStackTrace();
          }
          return routes;
      }
  
      @Override
      protected void onPostExecute(List&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt; result) {
  
          ArrayList&lt;LatLng&gt; points = null;
          PolylineOptions lineOptions = null;
  
          for (int i = 0; i &lt; result.size(); i++) {
              points = new ArrayList&lt;LatLng&gt;();
              lineOptions = new PolylineOptions();
  
              List&lt;HashMap&lt;String, String&gt;&gt; path = result.get(i);
  
              for (int j = 0; j &lt; path.size(); j++) {
                  HashMap&lt;String, String&gt; point = path.get(j);
  
                  double lat = Double.parseDouble(point.get("lat"));
                  double lng = Double.parseDouble(point.get("lng"));
                  LatLng position = new LatLng(lat, lng);
  
                  points.add(position);
              }
  
              lineOptions.addAll(points);
              lineOptions.width(12);
              lineOptions.color(Color.RED);
          }
  
          // Drawing polyline in the Google Map for the i-th route
          mMap.addPolyline(lineOptions);
      }
  }
  
  @Override
  public void onMapReady(GoogleMap googleMap) {
      mMap = googleMap;
  
      LatLng AT = new LatLng(20.039413, 74.479977);
      mMap.addMarker(new MarkerOptions().position(AT).title("Marker in Yeola"));
      mMap.moveCamera(CameraUpdateFactory.newLatLng(AT));
  }
  </code></pre>