Android is the way to draw a route on a map _android

Source: Internet
Author: User
Tags stub

The example in this article describes the way Android implements a roadmap on a map. Share to everyone for your reference. Specifically as follows:

Recently in the map to draw a road map, after a period of exploration, finally understand, in fact, quite simple, write a class to inherit overlay, and rewrite the draw method, in the draw method to draw a path. For Overaly, mark a point on the map or draw lines and the like to use Overlay,overlay equivalent to a cover, covered on the map, this cover to their own implementation so to inherit overlay.

Mapactivity.java is as follows:

Package net.blogjava.mobile.map; 
Import java.util.List; 
Import Android.app.AlertDialog; 
Import Android.graphics.Bitmap; 
Import Android.graphics.BitmapFactory; 
Import Android.graphics.Canvas; 
Import Android.graphics.Color; 
Import Android.graphics.Paint; 
Import Android.graphics.Path; 
Import Android.graphics.Point; 
Import android.location.Address; 
Import Android.location.Geocoder; 
Import Android.os.Bundle; 
Import Android.view.Menu; 
Import Com.google.android.maps.GeoPoint; 
Import com.google.android.maps.MapActivity; 
Import Com.google.android.maps.MapController; 
Import Com.google.android.maps.MapView; 
Import Com.google.android.maps.Overlay; 
Import com.google.android.maps.Projection; public class Main extends Mapactivity {private Geopoint gpoint1, Gpoint2, gpoint3;//connection point @Override public void Oncrea 
 Te (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 Setcontentview (R.layout.main); 
 Mapview Mapview = (mapview) Findviewbyid (R.id.mapview); Mapview. Setclickable (True); 
 Mapview.setbuiltinzoomcontrols (TRUE); 
 Mapcontroller Mapcontroller = Mapview.getcontroller (); Mapview.settraffic (TRUE);//traffic map//Mapview.setsatellite (TRUE);//satellite map//Mapview.setstreetview (TRUE)//Street View Myoverlay my 
 Overlay = new Myoverlay (); 
 Mapview.getoverlays (). Add (Myoverlay);  
 Mapcontroller.setzoom (15);//initial magnification gpoint1 = new Geopoint (int) (24.477384 * 1000000), (int) (118.158216 * 1000000)); 
 Gpoint2 = new Geopoint ((int) (24.488967 * 1000000), (int) (118.144277 * 1000000)); 
 Gpoint3 = new Geopoint ((int) (24.491091 * 1000000), (int) (118.136781 * 1000000)); 
Mapcontroller.animateto (GPOINT1); 
@Override protected Boolean isroutedisplayed () {//TODO auto-generated method stub return false;  Class Myoverlay extends Overlay {@Override public void Draw (Canvas Canvas, Mapview Mapview, Boolean shadow) {// 
 TODO auto-generated Method Stub Super.draw (canvas, Mapview, shadow); 
 Brush Paint Paint = new Paint (); Paint.setcolor (color.red); 
 Paint.setdither (TRUE); 
 Paint.setstyle (Paint.Style.STROKE); 
 Paint.setstrokejoin (Paint.Join.ROUND); 
 Paint.setstrokecap (Paint.Cap.ROUND); 
 Paint.setstrokewidth (2); 
 Projection projection = Mapview.getprojection (); 
 Point P1 = new Point (); 
 Point P2 = new Point (); 
 Point p3 = new Point (); 
 Projection.topixels (Gpoint1, p1); 
 Projection.topixels (Gpoint2, p2); 
 Projection.topixels (Gpoint3, p3); 
 Path PATH = new Path (); 
 Path.moveto (p1.x, P1.Y); 
 Path.lineto (p2.x, P2.Y); 
 Path.lineto (p3.x, P3.Y);

 Canvas.drawpath (path, paint);//Draw The path}}}

Main.xml is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
 android:orientation=" vertical "android:layout_width=" fill_parent " 
 android:layout_height=" Fill_parent "> 
 <com.google.android.maps.mapview 
 android:id=" @+id/mapview "android:layout_width=" Fill_parent " 
 android:layout_height=" fill_parent " 
 android:apikey=" 0ib7kn70qp1lt216hhb-jmhj8gltie4p63o77kq "/> 
</LinearLayout>

Finally, don't forget to add a privilege:

<uses-permission android:name= "Android.permission.INTERNET"/>

Between <applacation></applacation> add <uses-library android:name= "Com.google.android.maps"/>

To draw a road map:

/** * By parsing the XML returned by Google Map, draw the roadmap in map/public void Drawroute () {String URL = "Http://maps.google.com/maps/api/directi ons/xml?origin=23.055291,113.391802 "+" &destination=23.046604,113.397510&sensor=false&mode=walking " 
 ; 
 HttpGet get = new HttpGet (URL); 
 String strresult = ""; 
  try {httpparams httpparameters = new Basichttpparams (); 
  Httpconnectionparams.setconnectiontimeout (Httpparameters, 3000); 
  HttpClient httpclient = new Defaulthttpclient (httpparameters); 
  HttpResponse HttpResponse = null; 
  HttpResponse = Httpclient.execute (get); if (Httpresponse.getstatusline (). Getstatuscode () = = Strresult = entityutils.tostring (httpresponse.getentity ()) 
  ; 
 The catch (Exception e) {return; } if ( -1 = Strresult.indexof ("<status>OK</status>")) {Toast.maketext (this, "Get the navigation route failed!", Toast.length_sho 
  RT). Show (); 
  This.finish (); 
 Return 
 int pos = Strresult.indexof ("<overview_polyline>"); pos = Strresult.indexof ("<poiNts> ", POS + 1); 
 int pos2 = Strresult.indexof ("</points>", POS); 
 strresult = strresult.substring (pos + 8, POS2); 
 List<geopoint> points = Decodepoly (strresult); 
 Myoverlay moverlay = new Myoverlay (points); 
 list<overlay> overlays = Mmapview.getoverlays (); 
 Overlays.add (Moverlay); 
 if (Points.size () >= 2) {Mmapcontroller.animateto (points.get (0)); 
 } mmapview.invalidate (); /** * resolves overview_polyline route encoding in XML * * @param encoded * @return * * Private list<geopoint> Decodepoly (stri 
 NG encoded) {list<geopoint> poly = new arraylist<geopoint> (); 
 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; 
  Geopoint p = new Geopoint (int) ((double) lat/1e5 * 1E6), (int) ((double) lng/1e5) * 1E6)); 
 Poly.add (P); 
Return poly;

 }

I hope this article will help you with your Android program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.