Google Directions JSON Response在“ LEGS”数组中有一个名为Distance和Duration的对象,如下所示:
"legs": [ { "distance": { "text": "0.3 km", "value": 263 }, "duration": { "text": "1 min", "value": 52 }, ....... //omitted value ]因此,您可以尝试将距离和持续时间(如果需要)解析到您的应用中。也许像这样:
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 }哦,把腿数组解析放在ArrayTempatMakan解析中。我可以使用Jackson
JSON解析器获取距离和持续时间,因此您应该能够使用org.JSON解析器来实现。
祝你好运^^
编辑:
In this
getDirection (JSonObject jObj)method You successfully retrieve
objLegshere, 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
jarakand
durasi.
Inside your
JSONHelper.java, the
getTempatMakanAllmethod, 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 pre or modify your project,
you would have to figure out yourself :)
P.S : Skripsi or Tugas Kuliah?



