title: How get total distance and time from waypoints
body: <p>I try to build an app that shows me a route with waypoints and display me in textview Distance and travel time.</p>
  
  <p>my problem is the follow, below I have a code fragment, for calculate <code>total distance</code> and <code>totalSeconds</code>, before my app, only show me time and distance between two points, now with this code, it calculates distance and time from waypoints</p>
  
  <p>This is the code fragment  for I get totalDistance and totalSeconds from all waypoints</p>
  
  <p><a href="https://maps.googleapis.com/maps/api/directions/json?origin=6.3415247,-75.5577208&amp;destination=6.3498971,-75.5567523&amp;waypoints=optimize:true%7C6.3453442,-75.5516705%7C6.3483309,-75.5495995%7C6.3456380,-75.5491314%7C6.3455911,-75.5470564%7C6.3371096,-75.5501655%7C6.3415247,-75.5577208&amp;key=AIzaSyDLCO4H7Bsw3OgcjYHEOnl8j5WkWLjABow" rel="nofollow noreferrer">JSON File</a></p>
  
  <pre><code>public class DirectionFinder {
      private static final String DIRECTION_URL_API = "https://maps.googleapis.com/maps/api/directions/json?";
      private static final String GOOGLE_API_KEY = "AIzaSyDLCO4H7Bsw3OgcjYHEOnl8j5WkWLjABow";
      private DirectionFinderListener listener;
      private String origin;
      private String destination;
      private String waypoints;
  
  
      public DirectionFinder(DirectionFinderListener listener, String origin, String destination, String waypoints) {
          this.listener = listener;
          this.origin = origin;
          this.destination = destination;
          this.waypoints = waypoints;
  
      }
  
      public void execute() throws UnsupportedEncodingException {
          listener.onDirectionFinderStart();
          new DownloadRawData().execute(createUrl());
      }
  
      private String createUrl() throws UnsupportedEncodingException {
          String urlOrigin = URLEncoder.encode(origin, "utf-8");
          String urlDestination = URLEncoder.encode(destination, "utf-8");
          String urlWaypoints = URLEncoder.encode(waypoints, "utf-8");
  
  
          return DIRECTION_URL_API + "origin=" + urlOrigin + "&amp;destination=" + urlDestination + "&amp;waypoints=optimize:true" + urlWaypoints + "&amp;key=" + GOOGLE_API_KEY;
      }
  
      private class DownloadRawData extends AsyncTask&lt;String, Void, String&gt; {
  
          @Override
          protected String doInBackground(String... params) {
              String link = params[0];
              try {
                  URL url = new URL(link);
                  InputStream is = url.openConnection().getInputStream();
                  StringBuffer buffer = new StringBuffer();
                  BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  
                  String line;
                  while ((line = reader.readLine()) != null) {
                      buffer.append(line + "\n");
                  }
  
                  return buffer.toString();
  
              } catch (MalformedURLException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              return null;
          }
  
          @Override
          protected void onPostExecute(String res) {
              try {
                  parseJSon(res);
              } catch (JSONException e) {
                  e.printStackTrace();
              }
          }
      }
  
  
     private void parseJSon(String data) throws JSONException {
          if (data == null)
              return;
  
          List&lt;Route&gt; routes = new ArrayList&lt;Route&gt;();
          JSONObject jsonData = new JSONObject(data);
          JSONArray jsonRoutes = jsonData.getJSONArray("routes");
         long totalDistance = 0;
         int totalSeconds = 0;
  
         JSONObject jsonDistance = null;
         JSONObject jsonDuration = null;
          for (int i = 0; i &lt; jsonRoutes.length(); i++) {
              JSONObject jsonRoute = jsonRoutes.getJSONObject(i);
              Route route = new Route();
  
              JSONObject overview_polylineJson = jsonRoute.getJSONObject("overview_polyline");
              JSONArray jsonLegs = jsonRoute.getJSONArray("legs");
  
  
  
  
              for (int j = 0; j &lt; jsonLegs.length(); j++) {
  
                  jsonDistance = ((JSONObject) jsonLegs.get(j)).getJSONObject("distance");
                  totalDistance = totalDistance + Long.parseLong(jsonDistance.getString("value"));
  
                  /** Getting duration from the json data */
                  jsonDuration = ((JSONObject) jsonLegs.get(j)).getJSONObject("duration");
                  totalSeconds = totalSeconds + Integer.parseInt(jsonDuration.getString("value"));
  
  
  
  
              }
              route.distance = new Distance(jsonDistance.getString("text"), jsonDistance.getInt("value"));
              route.duration = new Duration(jsonDuration.getString("text"), jsonDuration.getInt("value"));
  
  
  
              route.points = decodePolyLine(overview_polylineJson.getString("points"));
  
              routes.add(route);
          }
  
         double dist = totalDistance / 1000.0;
         Log.d("distance", "Calculated distance:" + dist);
  
         int days = totalSeconds / 86400;
         int hours = (totalSeconds - days * 86400) / 3600;
         int minutes = (totalSeconds - days * 86400 - hours * 3600) / 60;
         int seconds = totalSeconds - days * 86400 - hours * 3600 - minutes * 60;
         Log.d("duration", days + " days " + hours + " hours " + minutes + " mins" + seconds + " seconds");
  
  
         listener.onDirectionFinderSuccess(routes);
      }
  </code></pre>
  
  <p>The problem is, I need to get the totalDistance and totalSeconds from before fragment, how I do to get this data, I don't know how I do, below is my before code to get data</p>
  
  <pre><code>route.distance = new Distance(jsonDistance.getString("text"), jsonDistance.getInt("value"));
  
      route.duration = new Duration(jsonDuration.getString("text"), jsonDuration.getInt("value"));
  </code></pre>