Android Google Map V2 Direction -
i trying driving direction between 2 positions -
latlng(15.244,45.85555) , latlng(78.459,97.4666).
the code have tried -
polyline line = mmap.addpolyline(new polylineoptions(). add(new latlng(15.244,45.85555), new latlng(78.459,97.4666)) .width(5).color(color.red));
but draws straight line between 2 points .
is there other method/way driving directions between these 2 points.
try way:
create gmapv2direction
class
public class gmapv2direction { public final static string mode_driving = "driving"; public final static string mode_walking = "walking"; public final static string mode_bicycling = "bicycling"; arraylist<string> str=new arraylist<string>(); 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; system.out.print(url); 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; }
}
defined in activity
like
public final string mode_driving = "driving"; public final string mode_walking = "walking"; public final string mode_bicycling = "bicycling";
now, implement asynctask
this:
class asynctask1 extends asynctask<string, string, void> { string mode_in = ""; int duration ; string distance; @override protected void onpreexecute() { super.onpreexecute(); mprogressdialog = new progressdialog(main2.this); mprogressdialog.setmessage("please wait.."); mprogressdialog.setprogressstyle(progressdialog.style_spinner); mprogressdialog.setcancelable(false); mprogressdialog.show(); } @override protected void doinbackground(string... aurl) { try { try { mode_in = aurl[0]; document doc = md.getdocument(fromposition, toposition,aurl[0]); duration = md.getdurationvalue(doc); distance = md.getdistancetext(doc); string start_address = md.getstartaddress(doc); string copy_right = md.getcopyrights(doc); system.out.print(duration + distance + start_address + copy_right); directionpoint = md.getdirection(doc); rectline = new polylineoptions().width(2); } catch (exception e) { e.printstacktrace(); } } catch (exception e) { } return null; } @override protected void onpostexecute(void result) { mprogressdialog.dismiss(); if (directionpoint.size() > 0) { try { (int = 0; < directionpoint.size(); i++) { rectline.add(directionpoint.get(i)); } if (rbdriving.ischecked()) { mode = mode_driving; rectline.color(color.red); } else if (rbbicycling.ischecked()) { mode = mode_bicycling; rectline.color(color.green); } else if (rbwalking.ischecked()) { mode = mode_walking; rectline.color(color.blue); } tv_distance_time.settext("duration: "+duration +" ,distance: "+distance); mmap.addpolyline(rectline); } catch (exception e) { e.printstacktrace(); } } else { alertdialog.builder builder = new alertdialog.builder( getparent()); builder.settitle("no route found"); builder.setmessage("no route found"); builder.setpositivebutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } } }
called asynctask
like
new asynctask1().execute(mode);
hope works you.