Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Firstly you can get the directions between two location co-ordinates using the Google Directions API.</p> <pre><code>public static ArrayList getDirections(double lat1, double lon1, double lat2, double lon2) { String url = "http://maps.googleapis.com/maps/api/directions/xml?origin=" +lat1 + "," + lon1 + "&amp;destination=" + lat2 + "," + lon2 + "&amp;sensor=false&amp;units=metric"; String tag[] = { "lat", "lng" }; ArrayList list_of_geopoints = new ArrayList(); HttpResponse response = null; try { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); response = httpClient.execute(httpPost, localContext); InputStream in = response.getEntity().getContent(); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.parse(in); if (doc != null) { NodeList nl1, nl2; nl1 = doc.getElementsByTagName(tag[0]); nl2 = doc.getElementsByTagName(tag[1]); if (nl1.getLength() &gt; 0) { list_of_geopoints = new ArrayList(); for (int i = 0; i &lt; nl1.getLength(); i++) { Node node1 = nl1.item(i); Node node2 = nl2.item(i); double lat = Double.parseDouble(node1.getTextContent()); double lng = Double.parseDouble(node2.getTextContent()); list_of_geopoints.add(new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6))); } } else { // No points found } } } catch (Exception e) { e.printStackTrace(); } return list_of_geopoints; } </code></pre> <p>Once a MapView layout is created in your android app, you can include this customized Overlay class.</p> <pre><code>public class MyOverlay extends Overlay { private ArrayList all_geo_points; public MyOverlay(ArrayList allGeoPoints) { super(); this.all_geo_points = allGeoPoints; } @Override public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) { super.draw(canvas, mv, shadow); drawPath(mv, canvas); return true; } public void drawPath(MapView mv, Canvas canvas) { int xPrev = -1, yPrev = -1, xNow = -1, yNow = -1; Paint paint = new Paint(); paint.setColor(Color.BLUE); paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setStrokeWidth(4); paint.setAlpha(100); if (all_geo_points != null) for (int i = 0; i &lt; all_geo_points.size() - 4; i++) { GeoPoint gp = all_geo_points.get(i); Point point = new Point(); mv.getProjection().toPixels(gp, point); xNow = point.x; yNow = point.y; if (xPrev != -1) { canvas.drawLine(xPrev, yPrev, xNow, yNow, paint); } xPrev = xNow; yPrev = yNow; } } } </code></pre> <p>The getDirections() function can be called right before before adding this Overlay to the MapView’s overlays.</p> <pre><code>MapView mv = (MapView) findViewById(R.id.mvGoogle); mv.setBuiltInZoomControls(true); MapController mc = mv.getController(); ArrayList all_geo_points = getDirections(17.3849, 78.4866, 28.63491, 77.22461); GeoPoint moveTo = all_geo_points.get(0); mc.animateTo(moveTo); mc.setZoom(12); mv.getOverlays().add(new MyOverlay(all_geo_points)); </code></pre>
 

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