Issue in draw route with waypoints with passing origin and destination address android -
hello friends want draw route origin , destination address including waypoints in android below code
string url="https://maps.googleapis.com/maps/api/directions/json?origin=toronto&destination=mississauga&waypoints=2970%20drew%20rd,%20suite%20211%20mississauga%20l4t%200a6%20ontario%20canada|6665%20tomken%20road,%20suite%20201%20mississauga%20l5t%202c4%20ontario%20canada"; downloadtask downloadtask = new downloadtask(); downloadtask.execute(url); private class downloadtask extends asynctask<string, void, string>{ // downloading data in non-ui thread @override protected string doinbackground(string... url) { // storing data web service string data = ""; try{ // fetching data web service data = downloadurl(url[0]); }catch(exception e){ log.d("background task",e.tostring()); } return data; } // executes in ui thread, after execution of // doinbackground() @override protected void onpostexecute(string result) { super.onpostexecute(result); parsertask parsertask = new parsertask(); // invokes thread parsing json data parsertask.execute(result); }
}
private class parsertask extends asynctask<string, integer, list<list<hashmap<string,string>>> >{ // parsing data in non-ui thread @override protected list<list<hashmap<string, string>>> doinbackground(string... jsondata) { jsonobject jobject; list<list<hashmap<string, string>>> routes = null; try{ jobject = new jsonobject(jsondata[0]); directionsjsonparser parser = new directionsjsonparser(); // starts parsing data routes = parser.parse(jobject); }catch(exception e){ e.printstacktrace(); } return routes; } // executes in ui thread, after parsing process @override protected void onpostexecute(list<list<hashmap<string, string>>> result) { arraylist<latlng> points = null; polylineoptions lineoptions = null; // traversing through routes for(int i=0;i<result.size();i++){ points = new arraylist<latlng>(); lineoptions = new polylineoptions(); // fetching i-th route list<hashmap<string, string>> path = result.get(i); // fetching points in i-th route for(int j=0;j<path.size();j++){ hashmap<string,string> 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); } // adding points in route lineoptions lineoptions.addall(points); lineoptions.width(2); lineoptions.color(color.red); } // drawing polyline in google map i-th route mymap.addpolyline(lineoptions); }
}
private string downloadurl(string strurl) throws ioexception{ string data = ""; inputstream istream = null; httpurlconnection urlconnection = null; try{ url url = new url(strurl); system.out.println("download "+url); // creating http connection communicate url urlconnection = (httpurlconnection) url.openconnection(); // connecting url urlconnection.connect(); // reading data url 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 downloading url", e.tostring()); }finally{ istream.close(); urlconnection.disconnect(); } return data;
}
directionsjsonparser.java
public class directionsjsonparser { /** receives jsonobject , returns list of lists containing latitude , longitude */ public list<list<hashmap<string,string>>> parse(jsonobject jobject){ list<list<hashmap<string, string>>> routes = new arraylist<list<hashmap<string,string>>>(); jsonarray jroutes = null; jsonarray jlegs = null; jsonarray jsteps = null; try { jroutes = jobject.getjsonarray("routes"); /** traversing routes */ for(int i=0;i<jroutes.length();i++){ jlegs = ( (jsonobject)jroutes.get(i)).getjsonarray("legs"); list path = new arraylist<hashmap<string, string>>(); /** traversing legs */ for(int j=0;j<jlegs.length();j++){ jsteps = ( (jsonobject)jlegs.get(j)).getjsonarray("steps"); /** traversing steps */ for(int k=0;k<jsteps.length();k++){ string polyline = ""; polyline = (string)((jsonobject)((jsonobject)jsteps.get(k)).get("polyline")).get("points"); list<latlng> list = decodepoly(polyline); /** traversing points */ for(int l=0;l<list.size();l++){ hashmap<string, string> hm = new hashmap<string, string>(); 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 decode polyline points * courtesy : jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java * */ private list<latlng> decodepoly(string encoded) { list<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 p = new latlng((((double) lat / 1e5)), (((double) lng / 1e5))); poly.add(p); } return poly; }
}
and catch error
11-27 15:04:47.794: d/exception while downloading url(12797): java.io.filenotfoundexception: https://maps.googleapis.com/maps/api/directions/json?origin=toronto&destination=mississauga&sensor=false&waypoints=2970 drew rd, suite 211 mississauga l4t 0a6 ontario canada|6665 tomken road, suite 201 mississauga l5t 2c4 ontario canada
in downloadurl(string strurl) function
any idea how can solve ?
Comments
Post a Comment