I'm trying to implement a functionality in my app which uses the Google Maps API Directions to draw a path (from A to B) in the map. For some reason it seems that the application can't even make the request. It always takes the case of result.size()<1
protected void onPostExecute(List<List<HashMap<String, String>>> result) {
ArrayList<LatLng> points = null;
PolylineOptions lineOptions = null;
MarkerOptions markerOptions = new MarkerOptions();
String distance = "";
String duration = "";
dialogz.dismiss();
if(result.size()<1){
Toast.makeText(getBaseContext(), "Road name cannot be identified, Please choose a nearest location", Toast.LENGTH_SHORT).show();
return;
}
for(int i=0;i<result.size();i++){
points = new ArrayList<LatLng>();
lineOptions = new PolylineOptions();
List<HashMap<String, String>> path = result.get(i);
for(int j=0;j<path.size();j++){
HashMap<String,String> point = path.get(j);
if(j==0){
distance = (String)point.get("distance");
continue;
}else if(j==1){
duration = (String)point.get("duration");
continue;
}
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
lineOptions.width(18);
lineOptions.color(Color.parseColor("#2E2E2E"));
}
---and this is the code of getDirectionUrl----
private void getDirectionsUrl(LatLng picklatlng,LatLng droplatlng){
if(picklatlng!=null && droplatlng!=null) {
String str_origin = "origin=" + picklatlng.latitude + "," + picklatlng.longitude;
String str_dest = "destination=" + droplatlng.latitude + "," + droplatlng.longitude;
String sensor = "sensor=false";
String parameters = str_origin + "&" + str_dest + "&" + sensor;
String output = "json";
String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;
DownloadTask downloadTask = new DownloadTask();
downloadTask.execute(url);
}
}