Note that there are some explanatory texts on larger screens.

plurals
  1. POdrawing driving routes using waypoints on android ( Google maps, Google direction api, json parsing, decode google polyline)
    text
    copied!<p>drawPath() is used to draw polyloine on map. </p> <pre><code>public void drawPath(String result){ try { final JSONObject jsonObject = new JSONObject(result); JSONArray routeArray = jsonObject.getJSONArray("routes"); JSONObject routes = routeArray.getJSONObject(0); JSONObject overviewPolylines = routes.getJSONObject("overview_polyline"); String encodedString = overviewPolylines.getString("points"); String statusString = jsonObject.getString("status"); 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 = mMap.addPolyline(new PolylineOptions() .add(new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude, dest.longitude)) .width(4) .color(Color.GREEN)); } Log.d("Last latLng:", last.latitude + ", " + last.longitude ); }catch (JSONException e){ e.printStackTrace(); } catch(ArrayIndexOutOfBoundsException e) { System.err.println("Caught ArrayIndexOutOfBoundsException: "+ e.getMessage()); } } 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>Now my problem is route is not displayed correctly in the map as it has waypoints in the link. I am using it on android 4.2 </p> <p>My link is <a href="http://maps.googleapis.com/maps/api/directions/json?origin=18.XXX,73.XXXdestination=18.XXX,73.XXX&amp;sensor=false&amp;units=metric&amp;mode=driving&amp;waypoints=via:18.XXX,73.XXX" rel="noreferrer">http://maps.googleapis.com/maps/api/directions/json?origin=18.XXX,73.XXXdestination=18.XXX,73.XXX&amp;sensor=false&amp;units=metric&amp;mode=driving&amp;waypoints=via:18.XXX,73.XXX</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload