title: Drawing a route between 2 locations Google Maps API Android V2
body: <p>I was playing around with the Google Maps API V2 on android.
  Trying to get a path between 2 locations and doing this with the JSON Parsing.</p>
  
  <p>I am getting a route. and the route starts out how it should be. but then at one point it goes the wrong way. </p>
  
  <p>My end destination ends up wrong. And with some other locations my app just gets terminated.</p>
  
  <p>This is what i have done </p>
  
  <p>Here is my makeURL method</p>
  
  <pre><code>public String makeUrl(){
      StringBuilder urlString = new StringBuilder();
      urlString.append("http://maps.googleapis.com/maps/api/directions/json");
      urlString.append("?origin="); //start positie
      urlString.append(Double.toString(source.latitude));
      urlString.append(",");
      urlString.append(Double.toString(source.longitude));
      urlString.append("&amp;destination="); //eind positie
      urlString.append(Double.toString(dest.latitude));
      urlString.append(",");
      urlString.append(Double.toString(dest.longitude));
      urlString.append("&amp;sensor=false&amp;mode=driving");
  
      return urlString.toString();
  }
  </code></pre>
  
  <p>my JSON parser</p>
  
  <pre><code>public class JSONParser {
  
  static InputStream is = null;
  static JSONObject jObj = null;
  static String json = "";
  
  public JSONParser() {
      // TODO Auto-generated constructor stub
  }
  
  public String getJSONFromURL(String url){
  
      try {
          DefaultHttpClient httpClient = new DefaultHttpClient();
          HttpPost httpPost = new HttpPost(url);
  
          HttpResponse httpResponse = httpClient.execute(httpPost);
          HttpEntity httpEntity = httpResponse.getEntity();
  
          is = httpEntity.getContent();
      } catch(UnsupportedEncodingException e){
          e.printStackTrace();
      } catch (ClientProtocolException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      try {
          BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
          StringBuilder sb = new StringBuilder();
          String line = null;
  
          while((line = reader.readLine()) != null){
              sb.append(line + "\n");
              //Log.e("test: ", sb.toString());
          }
  
          json = sb.toString();
          is.close();
      } catch (Exception e) {
          // TODO Auto-generated catch block
          Log.e("buffer error", "Error converting result " + e.toString());
      }
  
      return json;
  }
  </code></pre>
  
  <p>I draw my Path with this method</p>
  
  <pre><code>public void drawPath(String result){
      try{
          final JSONObject json = new JSONObject(result);
          JSONArray routeArray = json.getJSONArray("routes");
          JSONObject routes = routeArray.getJSONObject(0);
  
          JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
          String encodedString = overviewPolylines.getString("points");
          Log.d("test: ", encodedString);
          List&lt;LatLng&gt; list = decodePoly(encodedString);
  
          LatLng last = null;
          for (int i = 0; i &lt; list.size()-1; i++) {
              LatLng src = list.get(i);
              LatLng dest = list.get(i+1);
              last = dest;
              Log.d("Last latLng:", last.latitude + ", " + last.longitude );
              Polyline line = googleMap.addPolyline(new PolylineOptions().add( 
                      new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude, dest.longitude))
                      .width(2)
                      .color(Color.BLUE));
          }
  
          Log.d("Last latLng:", last.latitude + ", " + last.longitude );
      }catch (JSONException e){
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>And I decode my JSON with</p>
  
  <pre><code>private List&lt;LatLng&gt; decodePoly(String encoded){
  
      List&lt;LatLng&gt; poly = new ArrayList&lt;LatLng&gt;();
      int index = 0;
      int length = encoded.length();
      int latitude = 0;
      int longitude = 0;
  
      while(index &lt; length){
          int b;
          int shift = 0;
          int result = 0;
  
          do {
              b = encoded.charAt(index++) - 63;
              result |= (b &amp; 0x1f) &lt;&lt; shift;
              shift += 5;
          } while (b &gt;= 0x20);
  
          int destLat = ((result &amp; 1) != 0 ? ~(result &gt;&gt; 1) : (result &gt;&gt; 1));
          latitude += destLat;
  
          shift = 0;
          result = 0;
          do {
              b = encoded.charAt(index++) - 63;
              result |= (b &amp; 0x1f) &lt;&lt; shift;
              shift += 5;
          } while (b &gt; 0x20);
  
          int destLong = ((result &amp; 1) != 0 ? ~(result &gt;&gt; 1) : (result &gt;&gt; 1));
          longitude += destLong;
  
          poly.add(new LatLng((latitude / 1E5),(longitude / 1E5) ));
      }
      return poly;
  }
  </code></pre>
  
  <p>And then coded with an AsyncTask</p>
  
  <p>Thanks in advance.</p>