Here is the code to help you.
String url= "http://maps.googleapis.com/maps/api/directions/json?origin=" + origin.latitude + "," + origin.longitude +"&destination=" + destination.latitude + "," + destination.longitude + "&sensor=false";
To get data using androidhttpclient do the following:
HttpResponse response; HttpGet request; AndroidHttpClient client = AndroidHttpClient.newInstance("somename"); request = new HttpGet(url); response = client.execute(request); InputStream source = response.getEntity().getContent(); String returnValue = buildStringIOutils(source); return returnValue;
where buildStringIOUtils:
private String buildStringIOutils(InputStream is) { try { return IOUtils.toString(is, "UTF-8"); } catch (IOException e) { e.printStackTrace(); return null; } }
Then you can extract the actual polyline from the JSON response with something like this:
JSONObject result = new JSONObject(returnValue); JSONArray routes = result.getJSONArray("routes"); long distanceForSegment = routes.getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONObject("distance").getInt("value"); JSONArray steps = routes.getJSONObject(0).getJSONArray("legs") .getJSONObject(0).getJSONArray("steps"); List<LatLng> lines = new ArrayList<LatLng>(); for(int i=0; i < steps.length(); i++) { String polyline = steps.getJSONObject(i).getJSONObject("polyline").getString("points"); for(LatLng p : decodePolyline(polyline)) { lines.add(p); } }
where is the decodePolyline method:
private List<LatLng> decodePolyline(String encoded) { List<LatLng> poly = new ArrayList<LatLng>(); int index = 0, len = encoded.length(); int lat = 0, lng = 0; while (index < len) { int b, shift = 0, result = 0; do { b = encoded.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lat += dlat; shift = 0; result = 0; do { b = encoded.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lng += dlng; LatLng p = new LatLng((double) lat / 1E5, (double) lng / 1E5); poly.add(p); } return poly; }
Then you can add a polyline to the map as follows:
Polyline polylineToAdd = mMap.addPolyline(new PolylineOptions().addAll(lines).width(3).color(Color.RED));
To change the mode, add this to the URL (see https://developers.google.com/maps/documentation/directions/ ): & amp; Mode = YOUR_MODE
Driving (default) indicates standard driving directions using the road network.
Walking on footpaths and sidewalks (where available).
A bike requests cycling routes along bike paths and preferred streets (where available).
transit requests through public transport routes (if any).
Edit: O "I would also like to receive traffic information such as busy routes, congestion, etc." I have not considered this, but my code should make you start very well.
Edit2: Found it in google pointers: "For driving directions: Cards for Business customers can specify the departure time to get the duration of the trip taking into account the current traffic conditions. The departure time should be set within a few minutes from the current time."