title: Decoding polyline with new Google Maps API
body: <p>I am drawing a route between two points in a map. I receive the points this way:</p>
  <pre><code>StringBuilder urlString = new StringBuilder();
      urlString.append(&quot;http://maps.googleapis.com/maps/api/directions/json&quot;);
      urlString.append(&quot;?origin=&quot;);// from
      urlString.append(Double.toString(src.latitude));
      urlString.append(&quot;,&quot;);
      urlString.append(Double.toString(src.longitude));
      urlString.append(&quot;&amp;destination=&quot;);// to
      urlString.append(Double.toString(dest.latitude));
      urlString.append(&quot;,&quot;);
      urlString.append(Double.toString(dest.longitude));
      urlString.append(&quot;&amp;sensor=false&amp;mode=&quot;);
      if (tipo != null) {
          urlString.append(tipo);
      }
  return urlString.toString;
  </code></pre>
  <p>I receive the response from Google, and get the JSON:</p>
  <pre><code>resp = new JSONObject(builder.toString());
                  Log.i(&quot;Location&quot;, &quot;Contenido del kml: &quot;+resp);
                  JSONArray routeObject = resp.getJSONArray(&quot;routes&quot;);
                  JSONObject routes = routeObject.getJSONObject(0);
                  JSONObject overviewPolylines = routes
                          .getJSONObject(&quot;overview_polyline&quot;);
                  String encodedString = overviewPolylines.getString(&quot;points&quot;);
                  ArrayList&lt;LatLng&gt; puntos=decodePoly(encodedString);
  </code></pre>
  <p>The response I get from google: (in JSON format):</p>
  <pre><code>Response: {&quot;status&quot;:&quot;OK&quot;,&quot;routes&quot;:[{&quot;waypoint_order&quot;:[],&quot;summary&quot;:&quot;R-3 and A-3&quot;,&quot;bounds&quot;:{&quot;southwest&quot;:{&quot;lng&quot;:-3.676540000000001,&quot;lat&quot;:40.00040000000001},&quot;northeast&quot;:{&quot;lng&quot;:-2.99933,&quot;lat&quot;:40.43357}},&quot;legs&quot;:[{&quot;duration&quot;:{&quot;value&quot;:3267,&quot;text&quot;:&quot;54 mins&quot;},&quot;distance&quot;:{&quot;value&quot;:85039,&quot;text&quot;:&quot;85.0 km&quot;},&quot;end_location&quot;:{&quot;lng&quot;:-2.99933,&quot;lat&quot;:40.00040000000001},&quot;start_address&quot;:&quot;Calle del General Díaz Porlier, 91, 28006 Madrid, Spain&quot;,&quot;end_address&quot;:&quot;Camino Veinticuatro, 2, 16400 Tarancón, Cuenca, Spain&quot;,&quot;start_location&quot;:{&quot;lng&quot;:-3.676540000000001,&quot;lat&quot;:40.43331000000001},&quot;via_waypoint&quot;:[],&quot;steps&quot;:[{&quot;html_instructions&quot;:&quot;Head &lt;b&gt;north&lt;\/b&gt; on &lt;b&gt;Calle del Gral Díaz Porlier&lt;\/b&gt; toward &lt;b&gt;Calle de Maldonado&lt;\/b&gt;&quot;,&quot;duration&quot;:{&quot;value&quot;:6,&quot;text&quot;:&quot;1 min&quot;},&quot;distance&quot;:{&quot;value&quot;:29,&quot;text&quot;:&quot;29 m&quot;},&quot;end_location&quot;:{&quot;lng&quot;:-3.676520000000001,&quot;lat&quot;:40.43357},&quot;polyline&quot;:{&quot;points&quot;:&quot;ecxuFjamUs@C&quot;},&quot;travel_mode&quot;:&quot;DRIVING&quot;,&quot;start_location&quot;:{&quot;lng&quot;:-3.676540000000001,&quot;lat&quot;:40.43331000000001}},{&quot;html_instructions&quot;:&quot;Take the 1st &lt;b&gt;right&lt;\/b&gt; onto &lt;b&gt;Calle de Maldonado&lt;\/b&gt;&quot;,&quot;duration&quot;:{&quot;value&quot;:62,&quot;text&quot;:&quot;1 min&quot;},&quot;distance&quot;:{&quot;value&quot;:266,&quot;text&quot;:&quot;0.3 km&quot;},&quot;end_location&quot;:{&quot;lng&quot;:-3.6734,&quot;lat&quot;:40.43345},&quot;polyline&quot;:{&quot;points&quot;:&quot;ydxuFfamUPkG@a@JuGGk@&quot;},&quot;travel_mode&quot;:&quot;DRIVING&quot;,&quot;start_location&quot;:{&quot;lng&quot;:-3.676520000000001,&quot;lat&quot;:40.43357}},{&quot;html_instructions&quot;:&quot;Take the 2nd &lt;b&gt;right&lt;\/b&gt; onto &lt;b&gt;Calle de Fco. Silvela&lt;\/b&gt;&quot;,&quot;duration&quot;:{&quot;value&quot;:57,&quot;text&quot;:&quot;1 min&quot;},&quot;distance&quot;:{&quot;value&quot;:245,&quot;text&quot;:&quot;0.2 km&quot;},&quot;end_location&quot;:{&quot;lng&quot;:-3.671830000000001,&quot;lat&quot;:40.4316},&quot;polyline&quot;:{&quot;points&quot;:&quot;adxuFvmlURQl@e@zAoATSTMf@]pBeBLK&quot;},&quot;travel_mode&quot;:&quot;DRIVING&quot;,&quot;start_location&quot;:{&quot;lng&quot;:-3.6734,&quot;lat&quot;:40.43345}},{&quot;html_instructions&quot;:&quot;Slight &lt;b&gt;right&lt;\/b&gt; onto &lt;b&gt;Calle de Francisco Silvela&lt;\/b&gt;&quot;,&quot;duration&quot;:{&quot;value&quot;:51,&quot;text&quot;:&quot;1 min&quot;},&quot;distance&quot;:{&quot;value&quot;:437,&quot;text&quot;:&quot;0.4 km&quot;},&quot;end_location&quot;:{&quot;lng&quot;:-3.66913,&quot;lat&quot;:40.42827},&quot;polyline&quot;:{&quot;points&quot;:&quot;oxwuF|clUJADAFCbBkAjAeA`BoA^[XUPMLMFEnAeALIdBuAZUPMXS&quot;},&quot;travel_mode&quot;:&quot;DRIVING&quot;,&quot;start_location&quot;:{&quot;lng&quot;:-3.671830000000001,&quot;lat&quot;:40.4316}},{&quot;html_instructions&quot;:&quot;Turn &lt;b&gt;right&lt;\/b&gt; onto &lt;b&gt;Pl. de Manuel Becerra&lt;\/b&gt;&quot;,&quot;duration&quot;:{&quot;value&quot;:32,&quot;text&quot;:&quot;1 min&quot;},&quot;distance&quot;:{&quot;value&quot;:154,&quot;text&quot;:&quot;0.2 km&quot;},&quot;end_location&quot;:{&quot;lng&quot;:-3.668380000000001,&quot;lat&quot;:40.42827},&quot;polyline&quot;:{&quot;points&quot;:&quot;ucwuF`skU@?@@@@@?@@@?FBD?F@FAFAFADEDCFGDGDIBI@I@K@KAI?IAGAGAGCGCC?ACEEEIGGEICI?I?G@IBGD&quot;},&quot;travel_mode&quot;:&quot;DRIVING&quot;,&quot;start_location&quot;:{&quot;lng&quot;:-3.66913,&quot;lat&quot;:40.42827}},{&quot;html_instructions&quot;:&quot;Turn &lt;b&gt;right&lt;\/b&gt; onto &lt;b&gt;Calle Alcala&lt;\/b&gt;&quot;,&quot;duration&quot;:{&quot;value&quot;:61,&quot;text&quot;:&quot;1 min&quot;},&quot;distance&quot;:{&quot;value&quot;:709,&quot;text&quot;:&quot;0.7 km&quot;},&quot;end_location&quot;:{&quot;lng&quot;:-3.66107,&quot;lat&quot;:40.43126000000001},&quot;polyline&quot;:{&quot;points&quot;:&quot;ucwuFjnkUo@}A_AcCiEeLKYq@mBYq@[o@u@kB?KAQ]aAI[EQQu@Gc@QwAM_BEa@AU&quot;},&quot;travel_mode&quot;:&quot;DRIVING&quot;,&quot;start_location&quot;:{&quot;lng&quot;:-3.668380000000001,&quot;lat&quot;:40.42827}},{&quot;html_instructions&quot;:&quot;Turn &lt;b&gt;right&lt;\/b&gt; onto the ramp to &lt;b&gt;M-30\/A-3\/A-4&lt;\/b&gt;&quot;,&quot;duration&quot;:{&quot;value&quot;:44,&quot;text&quot;:&quot;1 min&quot;},&quot;distance&quot;:{&quot;value&quot;:453,&quot;text&quot;:&quot;0.5 km&quot;},&quot;end_location&quot;:{&quot;lng&quot;:-3.660580000000001,&quot;lat&quot;:40.42720000000001},&quot;polyline&quot;:{&quot;points&quot;:&quot;kvwuFt`jU\\CjIg@nBKD?rCOvDY&quot;},&quot;travel_mode&quot;:&quot;DRIVING&quot;,&quot;start_location&quot;:{&quot;lng&quot;:-3.66107,&quot;lat&quot;:40.43126000000001}},{&quot;html_instructions&quot;:&quot;Continue straight&quot;,&quot;duration&quot;:{&quot;value&quot;:45,&quot;text&quot;:&quot;1 min&quot;},&quot;distance&quot;:{&quot;value&quot;:1009,&quot;text&quot;:&quot;1.0 km&quot;},&quot;end_location&quot;:{&quot;lng&quot;:-3.659120000000001,&quot;lat&quot;:40.41822000000001},&quot;polyline&quot;:{&quot;points&quot;:&quot;_}vuFr}iUzKgBvCe@t@KvGm@hDUbAMPCZElAM|D_@^En@EjBEd@Df@DnAD&quot;},&quot;travel_mode&quot;:&quot;DRIVING&quot;,&quot;start_location&quot;:{&quot;lng&quot;:-3.660580000000001,&quot;lat&quot;:40.42720000000001}},{&quot;html_instructions&quot;:&quot;Take exit &lt;b&gt;7B&lt;\/b&gt; to merge onto &lt;b&gt;M-23&lt;\/b&gt; toward &lt;b&gt;Vicálvaro\/R-3\/Valencia&lt;\/b&gt;&quot;,&quot;duration&quot;:{&quot;value&quot;:117,&quot;text&quot;:&quot;2 mins&quot;},&quot;distance&quot;:{&quot;value&quot;:2552,&quot;text&quot;:&quot;2.6 km&quot;},&quot;end_location&quot;:{&quot;lng&quot;:-3.63392,&quot;lat&quot;:40.41499},&quot;polyline&quot;:{&quot;points&quot;:&quot;{duuFntiUFHB@^PDBFFFDDFDFBDDFBH@BBJ@F@H?F@H?FAH?FAF?FADAFAFCDADADEFCFEDEDEDEBEBEBGBE@G@E@G?G?EAG?GCGAGCEEGEEEEEEGEICIEIAKCKAK?K?MDgA?G?E?KJq@BODY@CBODW?C@I@AFe@F_@V{An@gED[@C?EHe@Ho@@G?A^{BjA}HF[tAiGzBwIdAcEVgApAuFr@sEF]Ju@DW?EDUHa@f@qEFo@f@iGXmHDgFCgGG{DM
  </code></pre>
  <p>And finally, I decode the received string this way, which I found in plenty of answers here in Stack Overflow:</p>
  <pre><code>private ArrayList&lt;LatLng&gt; decodePoly(String encoded) {
  
      Log.i(&quot;Location&quot;, &quot;String received: &quot;+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 p = new LatLng((int) (((double) lat /1E5)* 1E6), (int) (((double) lng/1E5   * 1E6)));
          poly.add(p);
      }
     
     for(int i=0;i&lt;poly.size();i++){
         Log.i(&quot;Location&quot;, &quot;Point sent: Latitude: &quot;+poly.get(i).latitude+&quot; Longitude: &quot;+poly.get(i).longitude);
     }
      return poly;
  }
  </code></pre>
  <p>This is where I think the error is, because I set a for to see the points that are being decoded, and the answer is something like:</p>
  <pre><code>04-10 13:33:54.578: I/Location(25065): Point sent: Latitude: 90.0 Longitude: -110.0
  04-10 13:33:54.578: I/Location(25065): Point sent: Latitude: 90.0 Longitude: -120.0
  04-10 13:33:54.578: I/Location(25065): Point sent: Latitude: 90.0 Longitude: 100.0
  04-10 13:33:54.578: I/Location(25065): Point sent: Latitude: 90.0 Longitude: 120.0
  04-10 13:33:54.578: I/Location(25065): Point sent: Latitude: 90.0 Longitude: 40.0
  </code></pre>
  <p>Obviosly, these points are badly parsed. The result is, no line drawn anywhere.</p>
  <p>My question is... does anybody know how to parse the answer from Google, and translate it to LatLng objects (NOT GeoPoints Objects)?</p>
  <p>EDIT:</p>
  <p>This is the String that is about to be decoded:</p>
  <pre><code>04-10 13:50:51.608: I/Location(25065): String to decode: gcxuFjamUq@CPkGLwHJ}@hCuBdEeDf@SrJuHnG{Ed@Fr@q@AmAm@k@e@DaIaSiEuLqAwJ~Mw@~ZwDtZuC`FKbCVjA`ANdAs@nBw@Py@]c@oAT}DhC{PlBcM~GaY|DyS`BmPRy]yBu\UsNvAqObAuEnC}KpAqIh@gK[}NcByWXoRt@}GlDeWvFcq@nFi`@~G}\|@aDdDoLn`@}gAfe@upA`JyV|CiN~@uId@{ZyBwVkBmOwB{^AsNt@iWb@qYcCyS_DkPmAaK{@gRN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you mean the way the LatLng objects are created, you can see in the decodePoly method. These latLng objects are added to the PolylineOptions objects, and the this added to the map, this way:</p>
  <pre><code>PolylineOption ruta=new PolylineOptions();
  for(int i=0;i&lt;puntos.size();i++){
  ruta.add(new LatLng(puntos.get(i).latitude, puntos.get(i).longitude));                      
  }//puntos is an array where the array returned by the decodePoly method are stored                  
  ruta.color(Color.RED).width(7);                     
  Polyline polygon=mapa.addPolyline(ruta);
  </code></pre>