android - Google maps api v2 get directions -


i using google maps api v2 in android . have placed marker. how can directions between current location , marker.any great.

take @ code provided here in 1 of posts creating polyline google direction api, can change class gets data if want other source how it. create class responsible getting direction , parsing:

public class gmapv2direction { public final static string mode_driving = "driving"; public final static string mode_walking = "walking";  public gmapv2direction() { }  public document getdocument(latlng start, latlng end, string mode) {     string url = "http://maps.googleapis.com/maps/api/directions/xml?"              + "origin=" + start.latitude + "," + start.longitude               + "&destination=" + end.latitude + "," + end.longitude              + "&sensor=false&units=metric&mode="+ mode;      try {         httpclient httpclient = new defaulthttpclient();         httpcontext localcontext = new basichttpcontext();         httppost httppost = new httppost(url);         httpresponse response = httpclient.execute(httppost, localcontext);         inputstream in = response.getentity().getcontent();         documentbuilder builder = documentbuilderfactory.newinstance().newdocumentbuilder();         document doc = builder.parse(in);         return doc;     } catch (exception e) {         e.printstacktrace();     }     return null; }  public string getdurationtext (document doc) {     nodelist nl1 = doc.getelementsbytagname("duration");     node node1 = nl1.item(0);     nodelist nl2 = node1.getchildnodes();     node node2 = nl2.item(getnodeindex(nl2, "text"));     log.i("durationtext", node2.gettextcontent());     return node2.gettextcontent(); }  public int getdurationvalue (document doc) {     nodelist nl1 = doc.getelementsbytagname("duration");     node node1 = nl1.item(0);     nodelist nl2 = node1.getchildnodes();     node node2 = nl2.item(getnodeindex(nl2, "value"));     log.i("durationvalue", node2.gettextcontent());     return integer.parseint(node2.gettextcontent()); }  public string getdistancetext (document doc) {     nodelist nl1 = doc.getelementsbytagname("distance");     node node1 = nl1.item(0);     nodelist nl2 = node1.getchildnodes();     node node2 = nl2.item(getnodeindex(nl2, "text"));     log.i("distancetext", node2.gettextcontent());     return node2.gettextcontent(); }  public int getdistancevalue (document doc) {     nodelist nl1 = doc.getelementsbytagname("distance");     node node1 = nl1.item(0);     nodelist nl2 = node1.getchildnodes();     node node2 = nl2.item(getnodeindex(nl2, "value"));     log.i("distancevalue", node2.gettextcontent());     return integer.parseint(node2.gettextcontent()); }  public string getstartaddress (document doc) {     nodelist nl1 = doc.getelementsbytagname("start_address");     node node1 = nl1.item(0);     log.i("startaddress", node1.gettextcontent());     return node1.gettextcontent(); }  public string getendaddress (document doc) {     nodelist nl1 = doc.getelementsbytagname("end_address");     node node1 = nl1.item(0);     log.i("startaddress", node1.gettextcontent());     return node1.gettextcontent(); }  public string getcopyrights (document doc) {     nodelist nl1 = doc.getelementsbytagname("copyrights");     node node1 = nl1.item(0);     log.i("copyrights", node1.gettextcontent());     return node1.gettextcontent(); }  public arraylist<latlng> getdirection (document doc) {     nodelist nl1, nl2, nl3;     arraylist<latlng> listgeopoints = new arraylist<latlng>();     nl1 = doc.getelementsbytagname("step");     if (nl1.getlength() > 0) {         (int = 0; < nl1.getlength(); i++) {             node node1 = nl1.item(i);             nl2 = node1.getchildnodes();              node locationnode = nl2.item(getnodeindex(nl2, "start_location"));             nl3 = locationnode.getchildnodes();             node latnode = nl3.item(getnodeindex(nl3, "lat"));             double lat = double.parsedouble(latnode.gettextcontent());             node lngnode = nl3.item(getnodeindex(nl3, "lng"));             double lng = double.parsedouble(lngnode.gettextcontent());             listgeopoints.add(new latlng(lat, lng));              locationnode = nl2.item(getnodeindex(nl2, "polyline"));             nl3 = locationnode.getchildnodes();             latnode = nl3.item(getnodeindex(nl3, "points"));             arraylist<latlng> arr = decodepoly(latnode.gettextcontent());             for(int j = 0 ; j < arr.size() ; j++) {                 listgeopoints.add(new latlng(arr.get(j).latitude, arr.get(j).longitude));             }              locationnode = nl2.item(getnodeindex(nl2, "end_location"));             nl3 = locationnode.getchildnodes();             latnode = nl3.item(getnodeindex(nl3, "lat"));             lat = double.parsedouble(latnode.gettextcontent());             lngnode = nl3.item(getnodeindex(nl3, "lng"));             lng = double.parsedouble(lngnode.gettextcontent());             listgeopoints.add(new latlng(lat, lng));         }     }      return listgeopoints; }  private int getnodeindex(nodelist nl, string nodename) {     for(int = 0 ; < nl.getlength() ; i++) {         if(nl.item(i).getnodename().equals(nodename))             return i;     }     return -1; }  private arraylist<latlng> decodepoly(string encoded) {     arraylist<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;         {             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;         {             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 position = new latlng((double) lat / 1e5, (double) lng / 1e5);         poly.add(position);     }     return poly; } } 

create getdirections asynctask:

public class getdirectionsasynctask extends asynctask<map<string, string>, object, arraylist<latlng>> {  public static final string user_current_lat = "user_current_lat"; public static final string user_current_long = "user_current_long"; public static final string destination_lat = "destination_lat"; public static final string destination_long = "destination_long"; public static final string directions_mode = "directions_mode"; private mapfragmentactivity activity; private string url;  private exception exception;  private dialog progressdialog;  public getdirectionsasynctask(mapfragmentactivity activity /*string url*/)  {     super();     this.activity = activity;      //  this.url = url; }  public void onpreexecute() {     progressdialog = dialogutils.createprogressdialog(activity, activity.getstring(r.string.get_data_dialog_message));     progressdialog.show(); }  @override public void onpostexecute(arraylist<latlng> result) {     progressdialog.dismiss();      if (exception == null) {         activity.handlegetdirectionsresult(result);     } else {         processexception();     } }  @override protected arraylist<latlng> doinbackground(map<string, string>... params) {      map<string, string> parammap = params[0];     try{         latlng fromposition = new latlng(double.valueof(parammap.get(user_current_lat)) , double.valueof(parammap.get(user_current_long)));         latlng toposition = new latlng(double.valueof(parammap.get(destination_lat)) , double.valueof(parammap.get(destination_long)));         gmapv2direction md = new gmapv2direction();         document doc = md.getdocument(fromposition, toposition, parammap.get(directions_mode));         arraylist<latlng> directionpoints = md.getdirection(doc);         return directionpoints;      }     catch (exception e) {         exception = e;         return null;     } }  private void processexception() {     toast.maketext(activity, activity.getstring(r.string.error_when_retrieving_data), 3000).show(); }  } 

in activity create 2 methods:

public void handlegetdirectionsresult(arraylist<latlng> directionpoints) {     polyline newpolyline;     googlemap mmap = ((supportmapfragment)getsupportfragmentmanager().findfragmentbyid(r.id.map)).getmap();      polylineoptions rectline = new polylineoptions().width(3).color(color.red);      for(int = 0 ; < directionpoints.size() ; i++)      {                   rectline.add(directionpoints.get(i));     }     newpolyline = mmap.addpolyline(rectline); }   public void finddirections(double frompositiondoublelat, double frompositiondoublelong, double topositiondoublelat, double topositiondoublelong, string mode) {     map<string, string> map = new hashmap<string, string>();     map.put(getdirectionsasynctask.user_current_lat, string.valueof(frompositiondoublelat));     map.put(getdirectionsasynctask.user_current_long, string.valueof(frompositiondoublelong));     map.put(getdirectionsasynctask.destination_lat, string.valueof(topositiondoublelat));     map.put(getdirectionsasynctask.destination_long, string.valueof(topositiondoublelong));     map.put(getdirectionsasynctask.directions_mode, mode);      getdirectionsasynctask asynctask = new getdirectionsasynctask(this);     asynctask.execute(map);  } 

finally run method create polyline:

finddirections(appobj.getinstance().currentuserlocation.getlatitude(),                                                    appobj.getinstance().currentuserlocation.getlongitude(),                                                    clickmarkerlatlng.latitude, clickmarkerlatlng.longitude, gmapv2direction.mode_driving ); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -