-2

Do I need a parsing json from the following url below and put the data of distance and duration in a listview, possible?

http://maps.googleapis.com/maps/api/directions/json?origin=-25.3641,-49.2857&destination=-25.3928,-49.2728&region=en&sensor=false

user1265628
  • 5
  • 1
  • 6
  • Sure it's possible. It's just a matter of taking the JSON data and putting it into a listview. – Jack Apr 26 '12 at 19:09

1 Answers1

0

Your question is not very clear. you need to parse the json first. You can put the parsed data in to an ArrayList of Bundle. Do something like the following:

String response = getResponseFromGoogleMaps(); //this function will fetch the response. write it in your way
ArrayList<Bundle> list = new ArrayList<Bundle>();
try {
        JSONObject json = new JSONObject(response);
        JSONArray routes = json.getJSONArray("route");
        JSONArray legs = routes.getJSONArray(0);
        JSONArray steps = legs.getJSONArray(0);
        for(int i=0;i<steps.length();i++) {
            JSONObject singleStep = steps.getJSONObject(i);
            JSONObject duration = singleStep.getJSONObject("duration");
            Bundle dur = new Bundle();
            dur.putString("text", duration.getString("text"));
            dur.putString("value", duration.getString("value"));
            JSONObject distance = singleStep.getJSONObject("distance");
            Bundle dis = new Bundle();
            dis.putString("text", distance.getString("text"));
            dis.putString("value", distance.getString("value"));
            Bundle data = new Bundle();
            data.putBundle("duration", dur);
            data.putBundle("distance", dis);
            list.add(data);
        }
} catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
}

Then create your own List Adapter (which should extend from BaseAdapter) which handles the ArrayList<Bundle>. And you are done!

jamael
  • 398
  • 1
  • 4
  • 13