api - Android: How to draw multiple routes from source to destination on map? -


i able draw single route using polyline between 2 geo points on android map v2. don't know how draw multiple routes source destination tried add "alternatives=true" still getting 1 route on map. please me out how research on web, still can't able find easy understandable solution this.

googledirection.java

@suppresslint("newapi") public class googledirection {   public googledirection(context context) {      mcontext = context; }   public string request(latlng start, latlng end, string mode) {     final 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 + "alternatives=true";      if(islogging)         log.i("googledirection", "url : " + url);     new requesttask().execute(new string[]{ url });     return url; }  private class requesttask extends asynctask<string, void, document> {     protected document doinbackground(string... url) {         try {             httpclient httpclient = new defaulthttpclient();             httpcontext localcontext = new basichttpcontext();             httppost httppost = new httppost(url[0]);             httpresponse response = httpclient.execute(httppost, localcontext);             inputstream in = response.getentity().getcontent();             documentbuilder builder = documentbuilderfactory.newinstance().newdocumentbuilder();             return builder.parse(in);         } catch (ioexception e) {             e.printstacktrace();         } catch (parserconfigurationexception e) {             e.printstacktrace();         } catch (saxexception e) {             e.printstacktrace();         }          return null;     }      protected void onpostexecute(document doc) {         super.onpostexecute(doc);         if(mdirectionlistener != null)             mdirectionlistener.onresponse(getstatus(doc), doc, googledirection.this);     }      private string getstatus(document doc) {         nodelist nl1 = doc.getelementsbytagname("status");         node node1 = nl1.item(0);         if(islogging)             log.i("googledirection", "status : " + node1.gettextcontent());         return node1.gettextcontent();     } }  public void setlogging(boolean state) {     islogging = state; }  public string getstatus(document doc) {     nodelist nl1 = doc.getelementsbytagname("status");     node node1 = nl1.item(0);     if(islogging)         log.i("googledirection", "status : " + node1.gettextcontent());     return node1.gettextcontent(); }  public string[] getdurationtext(document doc) {     nodelist nl1 = doc.getelementsbytagname("duration");     string[] arr_str = new string[nl1.getlength() - 1];     for(int = 0 ; < nl1.getlength() - 1 ; i++) {         node node1 = nl1.item(i);         nodelist nl2 = node1.getchildnodes();         node node2 = nl2.item(getnodeindex(nl2, "text"));         arr_str[i] = node2.gettextcontent();         if(islogging)             log.i("googledirection", "durationtext : " + node2.gettextcontent());     }     return arr_str; }  public int[] getdurationvalue(document doc) {     nodelist nl1 = doc.getelementsbytagname("duration");     int[] arr_int = new int[nl1.getlength() - 1];     for(int = 0 ; < nl1.getlength() - 1 ; i++) {         node node1 = nl1.item(i);         nodelist nl2 = node1.getchildnodes();         node node2 = nl2.item(getnodeindex(nl2, "value"));         arr_int[i] = integer.parseint(node2.gettextcontent());         if(islogging)             log.i("googledirection", "duration : " + node2.gettextcontent());     }     return arr_int; }  public string gettotaldurationtext(document doc) {     nodelist nl1 = doc.getelementsbytagname("duration");     node node1 = nl1.item(nl1.getlength() - 1);     nodelist nl2 = node1.getchildnodes();     node node2 = nl2.item(getnodeindex(nl2, "text"));     if(islogging)         log.i("googledirection", "totalduration : " + node2.gettextcontent());     return node2.gettextcontent(); }  public int gettotaldurationvalue(document doc) {     nodelist nl1 = doc.getelementsbytagname("duration");     node node1 = nl1.item(nl1.getlength() - 1);     nodelist nl2 = node1.getchildnodes();     node node2 = nl2.item(getnodeindex(nl2, "value"));     if(islogging)         log.i("googledirection", "totalduration : " + node2.gettextcontent());     return integer.parseint(node2.gettextcontent()); }  public string[] getdistancetext(document doc) {     nodelist nl1 = doc.getelementsbytagname("distance");     string[] arr_str = new string[nl1.getlength() - 1];     for(int = 0 ; < nl1.getlength() - 1 ; i++) {         node node1 = nl1.item(i);         nodelist nl2 = node1.getchildnodes();         node node2 = nl2.item(getnodeindex(nl2, "text"));         arr_str[i] = node2.gettextcontent();         if(islogging)             log.i("googledirection", "durationtext : " + node2.gettextcontent());     }     return arr_str; }  public int[] getdistancevalue(document doc) {     nodelist nl1 = doc.getelementsbytagname("distance");     int[] arr_int = new int[nl1.getlength() - 1];     for(int = 0 ; < nl1.getlength() - 1 ; i++) {         node node1 = nl1.item(i);         nodelist nl2 = node1.getchildnodes();         node node2 = nl2.item(getnodeindex(nl2, "value"));         arr_int[i] = integer.parseint(node2.gettextcontent());         if(islogging)             log.i("googledirection", "duration : " + node2.gettextcontent());     }     return arr_int; }  public string gettotaldistancetext(document doc) {     nodelist nl1 = doc.getelementsbytagname("distance");     node node1 = nl1.item(nl1.getlength() - 1);     nodelist nl2 = node1.getchildnodes();     node node2 = nl2.item(getnodeindex(nl2, "text"));     if(islogging)         log.i("googledirection", "totalduration : " + node2.gettextcontent());     return node2.gettextcontent(); }  public int gettotaldistancevalue(document doc) {     nodelist nl1 = doc.getelementsbytagname("distance");     node node1 = nl1.item(nl1.getlength() - 1);     nodelist nl2 = node1.getchildnodes();     node node2 = nl2.item(getnodeindex(nl2, "value"));     if(islogging)         log.i("googledirection", "totalduration : " + node2.gettextcontent());     return integer.parseint(node2.gettextcontent()); }  public string getstartaddress(document doc) {     nodelist nl1 = doc.getelementsbytagname("start_address");     node node1 = nl1.item(0);     if(islogging)         log.i("googledirection", "startaddress : " + node1.gettextcontent());     return node1.gettextcontent(); }  public string getendaddress(document doc) {     nodelist nl1 = doc.getelementsbytagname("end_address");     node node1 = nl1.item(0);     if(islogging)         log.i("googledirection", "startaddress : " + node1.gettextcontent());     return node1.gettextcontent(); }  public string getcopyrights(document doc) {     nodelist nl1 = doc.getelementsbytagname("copyrights");     node node1 = nl1.item(0);     if(islogging)         log.i("googledirection", "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; }  public arraylist<latlng> getsection(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, "end_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));         }     }      return listgeopoints; }  public polylineoptions getpolyline(document doc, int width, int color) {     arraylist<latlng> arr_pos = getdirection(doc);     polylineoptions rectline = new polylineoptions().width(dptopx(width)).color(color);     for(int = 0 ; < arr_pos.size() ; i++)                 rectline.add(arr_pos.get(i));     return rectline; }  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; }  private int dptopx(int dp) {     displaymetrics displaymetrics = mcontext.getresources().getdisplaymetrics();     int px = math.round(dp * (displaymetrics.xdpi / displaymetrics.density_default));            return px; }  public void setondirectionresponselistener(ondirectionresponselistener listener) {     mdirectionlistener = listener; }  public void setonanimatelistener(onanimatelistener listener) {     manimatelistener = listener; }  public interface ondirectionresponselistener {     public void onresponse(string status, document doc, googledirection gd); }  public interface onanimatelistener {     public void onfinish();     public void onstart();     public void onprogress(int progress, int total); }  public void animatedirection(googlemap gm, arraylist<latlng> direction, int speed         , boolean cameralock, boolean iscameratilt, boolean iscamerazoom         , boolean drawmarker, markeroptions mo, boolean flatmarker         , boolean drawline, polylineoptions po) {     if(direction.size() > 1) {         isanimated = true;         animatepositionlist = direction;         animatespeed = speed;         this.drawmarker = drawmarker;         this.drawline = drawline;         this.flatmarker = flatmarker;         this.iscameratilt = iscameratilt;         this.iscamerazoom = iscamerazoom;         step = 0;         this.cameralock = cameralock;         this.gm = gm;          setcameraupdatespeed(speed);          beginposition = animatepositionlist.get(step);         endposition = animatepositionlist.get(step + 1);         animatemarkerposition = beginposition;          if(manimatelistener != null)             manimatelistener.onprogress(step, animatepositionlist.size());          if(cameralock) {             float bearing = getbearing(beginposition, endposition);             cameraposition.builder camerabuilder = new cameraposition.builder()                 .target(animatemarkerposition).bearing(bearing);              if(iscameratilt)                  camerabuilder.tilt(90);             else                  camerabuilder.tilt(gm.getcameraposition().tilt);              if(iscamerazoom)                  camerabuilder.zoom(zoom);             else                  camerabuilder.zoom(gm.getcameraposition().zoom);              cameraposition cameraposition = camerabuilder.build();             gm.animatecamera(cameraupdatefactory.newcameraposition(cameraposition));         }          if(drawmarker) {             if(mo != null)                 animatemarker = gm.addmarker(mo.position(beginposition));             else                  animatemarker = gm.addmarker(new markeroptions().position(beginposition));              if(flatmarker) {                 animatemarker.setflat(true);                  float rotation = getbearing(animatemarkerposition, endposition) + 180;                 animatemarker.setrotation(rotation);             }         }           if(drawline) {             if(po != null)                  animateline = gm.addpolyline(po.add(beginposition)                         .add(beginposition).add(endposition)                         .width(dptopx((int)po.getwidth())));             else                  animateline = gm.addpolyline(new polylineoptions()                         .width(dptopx(5)));         }          new handler().postdelayed(r, speed);         if(manimatelistener != null)             manimatelistener.onstart();     } }  public void cancelanimated() {     isanimated = false; }  public boolean isanimated() {     return isanimated; }  private runnable r = new runnable() {     public void run() {          animatemarkerposition = getnewposition(animatemarkerposition, endposition);          if(drawmarker)             animatemarker.setposition(animatemarkerposition);           if(drawline) {             list<latlng> points = animateline.getpoints();             points.add(animatemarkerposition);             animateline.setpoints(points);         }          if((animatemarkerposition.latitude == endposition.latitude                  && animatemarkerposition.longitude == endposition.longitude)) {             if(step == animatepositionlist.size() - 2) {                 isanimated = false;                 totalanimatedistance = 0;                 if(manimatelistener != null)                     manimatelistener.onfinish();             } else {                 step++;                 beginposition = animatepositionlist.get(step);                 endposition = animatepositionlist.get(step + 1);                 animatemarkerposition = beginposition;                  if(flatmarker && step + 3 < animatepositionlist.size() - 1) {                     float rotation = getbearing(animatemarkerposition, animatepositionlist.get(step + 3)) + 180;                     animatemarker.setrotation(rotation);                 }                  if(manimatelistener != null)                     manimatelistener.onprogress(step, animatepositionlist.size());             }         }          if(cameralock && (totalanimatedistance > animatecamera || !isanimated)) {             totalanimatedistance = 0;             float bearing = getbearing(beginposition, endposition);             cameraposition.builder camerabuilder = new cameraposition.builder()                 .target(animatemarkerposition).bearing(bearing);              if(iscameratilt)                  camerabuilder.tilt(90);             else                  camerabuilder.tilt(gm.getcameraposition().tilt);              if(iscamerazoom)                  camerabuilder.zoom(zoom);             else                  camerabuilder.zoom(gm.getcameraposition().zoom);              cameraposition cameraposition = camerabuilder.build();             gm.animatecamera(cameraupdatefactory.newcameraposition(cameraposition));          }          if(isanimated) {             new handler().postdelayed(r, animatespeed);         }     } };  public marker getanimatemarker() {     return animatemarker; }  public polyline getanimatepolyline() {     return animateline; }  private latlng getnewposition(latlng begin, latlng end) {     double lat = math.abs(begin.latitude - end.latitude);      double lng = math.abs(begin.longitude - end.longitude);      double dis = math.sqrt(math.pow(lat, 2) + math.pow(lng, 2));     if(dis >= animatedistance) {         double angle = -1;          if(begin.latitude <= end.latitude && begin.longitude <= end.longitude)             angle = math.todegrees(math.atan(lng / lat));         else if(begin.latitude > end.latitude && begin.longitude <= end.longitude)             angle = (90 - math.todegrees(math.atan(lng / lat))) + 90;         else if(begin.latitude > end.latitude && begin.longitude > end.longitude)             angle = math.todegrees(math.atan(lng / lat)) + 180;         else if(begin.latitude <= end.latitude && begin.longitude > end.longitude)             angle = (90 - math.todegrees(math.atan(lng / lat))) + 270;          double x = math.cos(math.toradians(angle)) * animatedistance;         double y = math.sin(math.toradians(angle)) * animatedistance;         totalanimatedistance += animatedistance;         double finallat = begin.latitude + x;         double finallng = begin.longitude + y;          return new latlng(finallat, finallng);     } else {         return end;     } }  private float getbearing(latlng begin, latlng end) {     double lat = math.abs(begin.latitude - end.latitude);      double lng = math.abs(begin.longitude - end.longitude);      if(begin.latitude < end.latitude && begin.longitude < end.longitude)         return (float)(math.todegrees(math.atan(lng / lat)));     else if(begin.latitude >= end.latitude && begin.longitude < end.longitude)         return (float)((90 - math.todegrees(math.atan(lng / lat))) + 90);     else if(begin.latitude >= end.latitude && begin.longitude >= end.longitude)         return  (float)(math.todegrees(math.atan(lng / lat)) + 180);     else if(begin.latitude < end.latitude && begin.longitude >= end.longitude)         return (float)((90 - math.todegrees(math.atan(lng / lat))) + 270);      return -1; }  public void setcameraupdatespeed(int speed) {            if(speed == speed_very_slow) {         animatedistance = 0.000005;         animatespeed = 20;         animatecamera = 0.0004;         zoom = 19;     } else if(speed == speed_slow) {         animatedistance = 0.00001;         animatespeed = 20;         animatecamera = 0.0008;         zoom = 18;     } else if(speed == speed_normal) {         animatedistance = 0.00005;         animatespeed = 20;         animatecamera = 0.002;         zoom = 16;     } else if(speed == speed_fast) {         animatedistance = 0.0001;         animatespeed = 20;         animatecamera = 0.004;         zoom = 15;     } else if(speed == speed_very_fast) {         animatedistance = 0.0005;         animatespeed = 20;         animatecamera = 0.004;         zoom = 13;     } else {         animatedistance = 0.00005;         animatespeed = 20;         animatecamera = 0.002;         zoom = 16;     } } } 

mainactivity.java

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_map);       gd = new googledirection(this);     gd.setondirectionresponselistener(new ondirectionresponselistener() {         public void onresponse(string status, document doc, googledirection gd) {             mdoc = doc;         polyline =  mgooglemap.addpolyline(gd.getpolyline(doc, 10, color.red));             }     });  route.setonclicklistener(new view.onclicklistener() {           @override         public void onclick(view view) {             // todo auto-generated method stub               latlng newlatlong1 = new latlng(la1d, lo1d);             latlng newlatlong2 = new latlng(la2d, lo2d);              gd.setlogging(true);             gd.request(newlatlong2, newlatlong1, googledirection.mode_driving);       }     }); 

you need use google directions api. hit given url using http request , json response. google service calculates direction between given locations.

sample code:

pathgooglemapactivity.java

import java.util.arraylist; import java.util.hashmap; import java.util.list;  import org.json.jsonobject;  import android.graphics.color; import android.os.asynctask; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.util.log;  import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions; import com.google.android.gms.maps.model.polylineoptions;  public class pathgooglemapactivity extends fragmentactivity {      private static final latlng lower_manhattan = new latlng(40.722543,             -73.998585);     private static final latlng brooklyn_bridge = new latlng(40.7057, -73.9964);     private static final latlng wall_street = new latlng(40.7064, -74.0094);      googlemap googlemap;     final string tag = "pathgooglemapactivity";      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_path_google_map);         supportmapfragment fm = (supportmapfragment) getsupportfragmentmanager()                 .findfragmentbyid(r.id.map);         googlemap = fm.getmap();          markeroptions options = new markeroptions();         options.position(lower_manhattan);         options.position(brooklyn_bridge);         options.position(wall_street);         googlemap.addmarker(options);         string url = getmapsapidirectionsurl();         readtask downloadtask = new readtask();         downloadtask.execute(url);          googlemap.movecamera(cameraupdatefactory.newlatlngzoom(brooklyn_bridge,                 13));         addmarkers();      }      private string getmapsapidirectionsurl() {         string waypoints = "waypoints=optimize:true|"                 + lower_manhattan.latitude + "," + lower_manhattan.longitude                 + "|" + "|" + brooklyn_bridge.latitude + ","                 + brooklyn_bridge.longitude + "|" + wall_street.latitude + ","                 + wall_street.longitude;          string sensor = "sensor=false";         string params = waypoints + "&" + sensor;         string output = "json";         string url = "https://maps.googleapis.com/maps/api/directions/"                 + output + "?" + params;         return url;     }      private void addmarkers() {         if (googlemap != null) {             googlemap.addmarker(new markeroptions().position(brooklyn_bridge)                     .title("first point"));             googlemap.addmarker(new markeroptions().position(lower_manhattan)                     .title("second point"));             googlemap.addmarker(new markeroptions().position(wall_street)                     .title("third point"));         }     }      private class readtask extends asynctask {         @override         protected string doinbackground(string... url) {             string data = "";             try {                 httpconnection http = new httpconnection();                 data = http.readurl(url[0]);             } catch (exception e) {                 log.d("background task", e.tostring());             }             return data;         }          @override         protected void onpostexecute(string result) {             super.onpostexecute(result);             new parsertask().execute(result);         }     }      private class parsertask extends             asynctask>>> {          @override         protected list>> doinbackground(                 string... jsondata) {              jsonobject jobject;             list>> routes = null;              try {                 jobject = new jsonobject(jsondata[0]);                 pathjsonparser parser = new pathjsonparser();                 routes = parser.parse(jobject);             } catch (exception e) {                 e.printstacktrace();             }             return routes;         }          @override         protected void onpostexecute(list>> routes) {             arraylist points = null;             polylineoptions polylineoptions = null;              // traversing through routes             (int = 0; < routes.size(); i++) {                 points = new arraylist();                 polylineoptions = new polylineoptions();                 list> path = routes.get(i);                  (int j = 0; j < path.size(); j++) {                     hashmap point = path.get(j);                      double lat = double.parsedouble(point.get("lat"));                     double lng = double.parsedouble(point.get("lng"));                     latlng position = new latlng(lat, lng);                      points.add(position);                 }                  polylineoptions.addall(points);                 polylineoptions.width(2);                 polylineoptions.color(color.blue);             }              googlemap.addpolyline(polylineoptions);         }     } } 

httpconnection.java

import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.url;  import android.util.log;  public class httpconnection {     public string readurl(string mapsapidirectionsurl) throws ioexception {         string data = "";         inputstream istream = null;         httpurlconnection urlconnection = null;         try {             url url = new url(mapsapidirectionsurl);             urlconnection = (httpurlconnection) url.openconnection();             urlconnection.connect();             istream = urlconnection.getinputstream();             bufferedreader br = new bufferedreader(new inputstreamreader(                     istream));             stringbuffer sb = new stringbuffer();             string line = "";             while ((line = br.readline()) != null) {                 sb.append(line);             }             data = sb.tostring();             br.close();         } catch (exception e) {             log.d("exception while reading url", e.tostring());         } {             istream.close();             urlconnection.disconnect();         }         return data;     }  } 

pathjsonparser.java

import java.util.arraylist; import java.util.hashmap; import java.util.list;  import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject;  import com.google.android.gms.maps.model.latlng;  public class pathjsonparser {      public list>> parse(jsonobject jobject) {         list>> routes = new arraylist>>();         jsonarray jroutes = null;         jsonarray jlegs = null;         jsonarray jsteps = null;         try {             jroutes = jobject.getjsonarray("routes");             /** traversing routes */             (int = 0; < jroutes.length(); i++) {                 jlegs = ((jsonobject) jroutes.get(i)).getjsonarray("legs");                 list> path = new arraylist>();                  /** traversing legs */                 (int j = 0; j < jlegs.length(); j++) {                     jsteps = ((jsonobject) jlegs.get(j)).getjsonarray("steps");                      /** traversing steps */                     (int k = 0; k < jsteps.length(); k++) {                         string polyline = "";                         polyline = (string) ((jsonobject) ((jsonobject) jsteps                                 .get(k)).get("polyline")).get("points");                         list list = decodepoly(polyline);                          /** traversing points */                         (int l = 0; l < list.size(); l++) {                             hashmap hm = new hashmap();                             hm.put("lat",                                     double.tostring(((latlng) list.get(l)).latitude));                             hm.put("lng",                                     double.tostring(((latlng) list.get(l)).longitude));                             path.add(hm);                         }                     }                     routes.add(path);                 }             }          } catch (jsonexception e) {             e.printstacktrace();         } catch (exception e) {         }         return routes;     }      /**      * method courtesy :      * jeffreysambells.com/2010/05/27      * /decoding-polylines-from-google-maps-direction-api-with-java      * */     private list decodepoly(string encoded) {          list poly = new arraylist();         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 p = new latlng((((double) lat / 1e5)),                     (((double) lng / 1e5)));             poly.add(p);         }         return poly;     } } 

for more details, please refer here.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -