title: Android - How to remove straight line from multiple routes on Google Maps API v2
body: <p>I have app which set multiple routes on the map.
  The problem is when I put two points in the map, he get the suggested routes but with one extra straight line.</p>
  
  <p>I found a solution. I just get the main tag for polyline - <code>overview_polyline</code></p>
  
  <p>In the code are the new changes :)</p>
  
  <p>pic multiple routes - <code>alternatives=true</code> - I set just one road</p>
  
  <p><img src="https://i.stack.imgur.com/BDrwU.jpg" alt="enter image description here"></p>
  
  <p>pic - if I set <code>alternatives=false</code> in xml link, the straight line disappears.</p>
  
  <p><img src="https://i.stack.imgur.com/JKYkK.jpg" alt="enter image description here"></p>
  
  <p>In the xml everything is fine.
  I looked in to the 2 xml files with <code>alternatives=false</code> and <code>alternatives=true</code> but there are identical.</p>
  
  <p><img src="https://i.stack.imgur.com/0qySa.jpg" alt="enter image description here"></p>
  
  <p><a href="http://maps.googleapis.com/maps/api/directions/xml?origin=43.209822,27.913685&amp;destination=42.694692,23.321743&amp;sensor=false&amp;units=metric&amp;mode=driving&amp;alternatives=true" rel="nofollow noreferrer">link to XML file</a></p>
  
  <p>Thanks in advance.</p>
  
  <p>this code display the information:</p>
  
  <pre><code> //Polyline
  private class GetRouteTask extends AsyncTask&lt;String, Void, String&gt; {
  
  
      String response = "";
  
  
      @Override
      protected String doInBackground(String... urls) {
          //Get All Route values
  
  
          v2GetRouteDirection = new GMapV2Direction();
  
              document = v2GetRouteDirection.getDocument(latLngFrom, latLngTo, GMapV2Direction.MODE_DRIVING);
  
  
          response = "Success";
          return response;
  
      }
  
      @Override
      protected void onPostExecute(String result) {
  
  
          if(document != null){
  
  
                  ArrayList&lt;LatLng&gt; directionPoint = v2GetRouteDirection.getDirection(document);
                  PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);
  
  
                  for (int i = 0; i &lt; directionPoint.size(); i++) {
                      rectLine.add(directionPoint.get(i));
  
                  }
  
  
  
                  // Adding route on the map
                  map.addPolyline(rectLine);
  
  
          }
  
  
      }
  
  }
  </code></pre>
  
  <p>this code get the tag information:</p>
  
  <pre><code>public class GMapV2Direction {
  
  
  
  public GMapV2Direction(){}
  
  public final static String MODE_DRIVING = "driving";
  public final static String MODE_WALKING = "walking";
  
  
  public static String error = null;
  
  
  public Document getDocument(LatLng start, LatLng end, String mode) {
      String url = "http://maps.googleapis.com/maps/api/directions/xml?"
              + "origin=" + start.latitude + "," + start.longitude
              + "&amp;destination=" + end.latitude + "," + end.longitude
              + "&amp;sensor=false&amp;units=metric&amp;mode=driving&amp;alternatives=true";
  
  
  
      ////////////////
  
      //Set TimeOuts
      HttpParams httpParameters = new BasicHttpParams();
      // Set the timeout in milliseconds until a connection is established.
      // The default value is zero, that means the timeout is not used.
      int timeoutConnection = 3000;
      HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
      // Set the default socket timeout (SO_TIMEOUT)
      // in milliseconds which is the timeout for waiting for data.
      int timeoutSocket = 5000;
      HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
  
  
  
      try {
  
          HttpClient httpClient = new DefaultHttpClient(httpParameters);
          HttpContext localContext = new BasicHttpContext();
          HttpGet httpGet = new HttpGet(url);
          HttpResponse response = httpClient.execute(httpGet, localContext);
          InputStream in = response.getEntity().getContent();
          DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
          Document doc = builder.parse(in);
          return doc;
  
  
  
      }
  
      catch (ClientProtocolException e) {
          error = "some";
          e.printStackTrace();
      } catch (IOException e) {
          error = "some";
          e.printStackTrace();
      }
  
      catch (Exception e) {
          error = "some";
          e.printStackTrace();
      }
  
  
      return null;
  }
  
  
  public ArrayList&lt;LatLng&gt; getDirection (Document doc) {
  
      NodeList error = doc.getElementsByTagName("status");
  
      int p;
      Node error1 = null;
      //взема последнич елемент на таг
      for(p = 0;p&lt;error.getLength();p++){
          error1 = error.item(p);
      }
  
      if(p==p){
          p--;
      }
  
      LatLng latLngZero = new LatLng(0.0, 0.0);
  
      ArrayList&lt;LatLng&gt; list = new ArrayList&lt;LatLng&gt;();
      list.add(latLngZero);
  
  
      ArrayList&lt;LatLng&gt; listGeopoints = null;
  
  
      if(error1.getFirstChild().getTextContent().equals("OK")) {
  
         //new
  
            NodeList routeTag,nl3;
  
              listGeopoints = new ArrayList&lt;LatLng&gt;();
  
              //get tag route
              routeTag = doc.getElementsByTagName("route");
  
  
              if (routeTag.getLength() &gt; 0) {
  
  
                  //get first eelemnt of route
                  Element routeElement = (Element) routeTag.item(0);
  
  
                  //get tag overview_polyline
                  NodeList polylineList = routeElement.getElementsByTagName("overview_polyline");
  
  
                  Node node1 = polylineList.item(0);
  
                  nl3 = node1.getChildNodes();
                  Node latNode = nl3.item(getNodeIndex(nl3, "points"));
                  List&lt;LatLng&gt; arr = decodePoly(latNode.getTextContent());
                  for (int j = 0; j &lt; arr.size(); j++) {
                      listGeopoints.add(new LatLng(arr.get(j).latitude, arr.get(j).longitude));
                  }
  
  
              }
     } 
       ////////////// 
       else{
          return list;
  
      }
  
      return listGeopoints;
  }
  
  
  
  
  private int getNodeIndex(NodeList nl, String nodename) {
      for(int i = 0 ; i &lt; nl.getLength() ; i++) {
          if(nl.item(i).getNodeName().equals(nodename))
              return i;
      }
      return -1;
  }
  
  private ArrayList&lt;LatLng&gt; decodePoly(String encoded) {
      ArrayList&lt;LatLng&gt; poly = new ArrayList&lt;LatLng&gt;();
      int index = 0, len = encoded.length();
      int lat = 0, lng = 0;
      while (index &lt; len) {
          int b, shift = 0, result = 0;
          do {
              b = encoded.charAt(index++) - 63;
              result |= (b &amp; 0x1f) &lt;&lt; shift;
              shift += 5;
          } while (b &gt;= 0x20);
          int dlat = ((result &amp; 1) != 0 ? ~(result &gt;&gt; 1) : (result &gt;&gt; 1));
          lat += dlat;
          shift = 0;
          result = 0;
          do {
              b = encoded.charAt(index++) - 63;
              result |= (b &amp; 0x1f) &lt;&lt; shift;
              shift += 5;
          } while (b &gt;= 0x20);
          int dlng = ((result &amp; 1) != 0 ? ~(result &gt;&gt; 1) : (result &gt;&gt; 1));
          lng += dlng;
  
          LatLng position = new LatLng((double) lat / 1E5, (double) lng / 1E5);
          poly.add(position);
      }
      return poly;
    }
  }
  </code></pre>