Subscribe For Free Updates!

We'll not spam mate! We promise.

Jun 27, 2012

Draw Routs on google maps in android

Views:

Draw Routs  on google maps in androidTo day i show you how

draw Rout between Two location on google maps in android

.

 So lets Start ...

Firstly you can get the directions between two location co-ordinates using the Google Directions API.
The example I have used in this post is this XML.
 You can change the parameters to the locations you wish and get the co-ordinates from this site.




The following method returns the list of GeoPoints from the request made using the directions API.

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 + "&destination=" + lat2 + "," + lon2 + "&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() > 0) { list_of_geopoints = new ArrayList(); for (int i = 0; i < 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; }




Once a MapView layout is created in your android app, you can include this customized Overlay class.
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 < 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;
            }
    }
}


The getDirections() function can be called right before before adding this Overlay to the MapView’s overlays.

MapView mv = (MapView) findViewById(R.id.mvGoogle);
mv.setBuiltInZoomControls(true);
MapController mc = mv.getController();
ArrayList all_geo_points=getDirections(10.154929, 76.390316, 10.015861,
76.341867); GeoPoint moveTo = all_geo_points.get(0); mc.animateTo(moveTo); mc.setZoom(12); mv.getOverlays().add(new MyOverlay(all_geo_points)); 

Plese Feel Free to Socializer This Post
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment

Become a Fan

visual studio learn