I have roughly tried to parse the JSON from Google's Distance matrix. But it is not showing the distance. My GPS location is not 0,0 That i'm sure of.
String distance = getMatrix(latitude,longitude);
My code for the function is:
private String getMatrix(double lat , double lang){
JSONObject jObj;
String getdistance = "";
String strUrl = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=" + Double.toString(my_lat) + "," + Double.toString(my_lang) +
"&destinations="+ Double.toString(lat) + "," + Double.toString(lang) + "&mode=walking&sensor=false";
String data = "";
InputStream reader = null;
HttpURLCoection urlCoection = null;
try{
URL url = new URL(strUrl);
urlCoection = (HttpURLCoection) url.openCoection();
urlCoection.coect();
reader = urlCoection.getInputStream();
int bRead = -1;
byte[] buffer = new byte[1024];
do{
bRead = reader.read(buffer, 0, 1024);
if(bRead==-1){
break;
}
data += new String(buffer, 0, bRead);
}while (true);
urlCoection.discoect();
}catch(Exception e){
}finally{
}
try{
jObj = new JSONObject(data);
JSONArray rowsArray = jObj.getJSONArray("rows");
JSONObject rows = rowsArray.getJSONObject(0);
JSONArray elementsArray = rows.getJSONArray("elements");
JSONObject newDisTimeOb = elementsArray.getJSONObject(0);
JSONObject distOb = newDisTimeOb.getJSONObject("distance");
getdistance = distOb.optString("text").toString();
}catch (JSONException e) {
e.printStackTrace();
}
retu getdistance;
}
