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.
This is what logcat reports:
This is the class that I am using to do it:
public class DrawPath {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
MainActivity actividadPrincipal;
public DrawPath(MainActivity actividadPrincipal){
this.actividadPrincipal = actividadPrincipal;
}
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;
do {
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;
do {
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;
}
public void drawPath(String result) {
try {
//Tranform the string into a json object
final JSONObject json = new JSONObject(result);
JSONArray routeArray = json.getJSONArray("routes");
JSONObject routes = routeArray.getJSONObject(0);
JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
String encodedString = overviewPolylines.getString("points");
List<LatLng> list = decodePoly(encodedString);
for(int z = 0; z<list.size()-1;z++){
LatLng src= list.get(z);
LatLng dest= list.get(z+1);
Polyline line = actividadPrincipal.googleMap.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude, dest.longitude))
.width(2)
.color(Color.BLUE).geodesic(true));
}
}
catch (JSONException e) {
}
}
// Parser Class
public String getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
json = sb.toString();
is.close();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return json;
}
//First of all we will get source and destination points between which we have to draw route. Then we will pass these attribute to getJSONFromUrl function.
public String makeURL (double sourcelat, double sourcelog, double destlat, double destlog ){
StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.googleapis.com/maps/api/directions/json");
urlString.append("?origin="+Double.toString(sourcelat)+","+Double.toString( sourcelog));// from
urlString.append("&destination="+Double.toString(destlat)+","+Double.toString(destlog));// to
urlString.append("&sensor=false");
urlString.append("&mode=driving");
urlString.append("&alternatives=true");
return urlString.toString();
}
}
Adapted from this Answer : Draw path between two points using Google Maps Android API v2
Now, I have checked the Directions Api Documentation and haven't found anything related to this problem.
This is how I call the functions above from the bottom of onCreate (I havent designed a button yet)
//test
DrawPath dPath = new DrawPath(this);
String path = dPath.makeURL(markers.get(0).marker.getPosition().latitude,
markers.get(0).marker.getPosition().longitude,
markers.get(1).marker.getPosition().latitude,
markers.get(1).marker.getPosition().longitude);
dPath.drawPath(path);
I have checked the link and I see nothing wrong with it. Perhaps I'm missing a permission? But it would throw an exception in that case.
I tried changing http to https and adding "&key=myApiKey" at the end of the request as sugested by @3amoura, but got same errors.
UPDATE
After fixing minor stuff and hardcoding the url to request to http://maps.googleapis.com/maps/api/directions/json?origin=Granada&destination=Malaga&sensor=false it works, so the problem is in the makeUrl, I must be making it wrong
The problem was in the way makeUrl was making the url, I changed it to a simpler way and now it works.
public String makeURL (double sourcelat, double sourcelog, double destlat, double destlog ){
String url = "https://maps.googleapis.com/maps/api/directions/json" +
"?origin="+Double.toString(sourcelat)+","+Double.toString(sourcelog) +
"&destination="+Double.toString(destlat)+","+Double.toString(destlog) +
"&sensor=false" +
"&mode=walking" +
"&alternatives=true";
return url;
}