Google Directions JSON Response has an object called Distance and Duration within "LEGS" array, like this :
"legs": [
{
"distance": {
"text": "0.3 km",
"value": 263
},
"duration": {
"text": "1 min",
"value": 52
},
....... //omitted value
]
Thus you can try parsing the distance and duration (if you need it) into your app.
Maybe like this :
JSONArray legs = jobject.getString("legs");
for (int i = 0; i < legs.length(); i++)
{
JSONObject legsObject = legs.getJSONObject(i);
JSONObject distanceObject = legsObject.getString("distance");
String distanceText = distanceObject.getString("text");
String distanceValue = String.valueOf(distanceObject.getString("value"));
//do anything else
}
oh, and put the legs array parsing inside the ArrayTempatMakan parsing. I can get the distance and duration using Jackson JSON parser, so you should be able to do it with org.JSON parser.
Good luck ^^
EDIT :
In this getDirection (JSONObject jObj)
method You successfully retrieve objLegs
here, you need to get the Distance and Duration within objLegs
JSONObject objRoute = jObj.getJSONArray(TAG_ROUTES).getJSONObject(0);
JSONObject objLegs = objRoute.getJSONArray(TAG_LEGS).getJSONObject(0);
JSONArray arraySteps = objLegs.getJSONArray(TAG_STEPS);
I think you should add this below objLegs
:
JSONObject objDistance = objLegs.getJSONObject("distance");
JSONObject objDuration = objLegs.getJSONObject("duration");
String text_distance = objDistance.getString("text");
Log.d("Distance", text_distance); //adds an entry to the logCat, Check whether the value of Distance is successfully taken.
String text_duration = objDuration.getString("text");
Log.d("Duration", text_duration); //adds an entry to the logCat, Check whether the value of Distance is successfully taken.
There, I believe you should be able to grab the value, after that, you can just add the value into your list result.
If you're still unable to insert distance and duration, please include the TempatMakan class on your post.
EDIT 2 :
Declare distance and duration as string inside your TempatMakan.Java :
private String durasi;
private String jarak;
inside your constructor :
public TempatMakan(int id, String nama, String alamat, String jarak, String durasi, double lat, double lng)
{
super();
this.id = id;
this.nama = nama;
this.alamat = alamat;
this.lat = lat;
this.lng = lng;
this.jarak = jarak;
this.durasi = durasi;
}
then declare the respective getters and setters for jarak
and durasi
.
Inside your JSONHelper.java
, the getTempatMakanAll
method, add these :
for (int i = 0; i < arrayTempatMakan.length(); i++)
{
JSONObject jobject = arrayTempatMakan.getJSONObject(i);
JSONObject objRoute = jobject.getJSONArray(TAG_ROUTES).getJSONObject(0);
JSONObject objLegs = objRoute.getJSONArray(TAG_LEGS).getJSONObject(0);
JSONObject objDistance = objLegs.getJSONObject("distance");
JSONObject objDuration = objLegs.getJSONObject("duration");
Log.d("log", "muter ke " + i);
listTempatMakan.add(
new TempatMakan(
jobject.getInt(TAG_ID),
jobject.getString(TAG_NAMA),
jobject.getString(TAG_ALAMAT),
objDistance.getString("text"),//jarak
objDuration.getString("text"),//durasi
jobject.getDouble(TAG_LAT),
jobject.getDouble(TAG_LNG)));
}
after that, just modify your listview adapter to display those values.
Good Luck, and Sorry, I can't give you another code or modify your project, you would have to figure out yourself :)
P.S : Skripsi or Tugas Kuliah?