title: how distance and duration actually computed in android google maps direction api?
body: <p>I am an android noob and i am exploring android maps api.I am referring this <a href="http://wptrafficanalyzer.in/blog/driving-distance-and-travel-time-duration-between-two-locations-in-google-map-android-api-v2/" rel="nofollow noreferrer">site</a>.
  I can understand everything except the distance and duration retrieval.
  The JSON parsing code is given below.</p>
  
  <pre><code>    public List&lt;List&lt;HashMap&lt;String,String&gt;&gt;&gt; parse(JSONObject jObject){
  
      List&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt; routes = new     ArrayList&lt;List&lt;HashMap&lt;String,String&gt;&gt;&gt;() ;
      JSONArray jRoutes = null;
      JSONArray jLegs = null;
      JSONArray jSteps = null;
      JSONObject jDistance = null;
      JSONObject jDuration = null;
  
      try {
  
          jRoutes = jObject.getJSONArray("routes");
  
          /** Traversing all routes */
          for(int i=0;i&lt;jRoutes.length();i++){
              jLegs = ( (JSONObject)jRoutes.get(i)).getJSONArray("legs");
  
              List&lt;HashMap&lt;String, String&gt;&gt; path = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
              /** Traversing all legs */
              for(int j=0;j&lt;jLegs.length();j++){
  
                  /** Getting distance from the json data */
                  jDistance = ((JSONObject) jLegs.get(j)).getJSONObject("distance");
                  HashMap&lt;String, String&gt; hmDistance = new HashMap&lt;String, String&gt;();
                  hmDistance.put("distance", jDistance.getString("text"));
  
                  /** Getting duration from the json data */
                  jDuration = ((JSONObject) jLegs.get(j)).getJSONObject("duration");
                  HashMap&lt;String, String&gt; hmDuration = new HashMap&lt;String, String&gt;();
                  hmDuration.put("duration", jDuration.getString("text"));
  
                  /** Adding distance object to the path */
                  path.add(hmDistance);
  
                  /** Adding duration object to the path */
                  path.add(hmDuration);
  
                  jSteps = ( (JSONObject)jLegs.get(j)).getJSONArray("steps");
  
                  /** Traversing all steps */
                  for(int k=0;k&lt;jSteps.length();k++){
                      String polyline = "";
                      polyline = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("polyline")).get("points");
                      List&lt;LatLng&gt; list = decodePoly(polyline);
  
                      /** Traversing all points */
                      for(int l=0;l&lt;list.size();l++){
                          HashMap&lt;String, String&gt; hm = new HashMap&lt;String, String&gt;();
                          hm.put("lat", Double.toString(((LatLng)list.get(l)).latitude) );
                          hm.put("lng", Double.toString(((LatLng)list.get(l)).longitude) );
                          path.add(hm);
                      }
                  }
              }
              routes.add(path);
          }
      } catch (JSONException e) {
         e.printStackTrace();
      }catch (Exception e){
      }
      return routes;
  }
  </code></pre>
  
  <p>They are getting distance and duration data for every single leg.The Android docs says , <strong><em>distance indicates the total distance covered by this leg</em></strong> and <strong><em>"duration indicates the total duration of this leg</em>"</strong>.So my assumption was, during final distance and duration computaion,
  all these datum would be added.</p>
  
  <p>This is the final code:</p>
  
  <pre><code>    protected void onPostExecute(List&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt; result) {
          ArrayList&lt;LatLng&gt; points = null;
          PolylineOptions lineOptions = null;
          MarkerOptions markerOptions = new MarkerOptions();
          String distance = "";
          String duration = "";
  
          if(result.size()&lt;1){
              Toast.makeText(getBaseContext(), "No Points", Toast.LENGTH_SHORT).show();
              return;
          }
  
          // Traversing through all the routes
          for(int i=0;i&lt;result.size();i++){
              points = new ArrayList&lt;LatLng&gt;();
              lineOptions = new PolylineOptions();
  
              // Fetching i-th route
              List&lt;HashMap&lt;String, String&gt;&gt; path = result.get(i);
  
              // Fetching all the points in i-th route
              for(int j=0;j&lt;path.size();j++){
                  HashMap&lt;String,String&gt; point = path.get(j);
  
                  if(j==0){    // Get distance from the list
                      distance = (String)point.get("distance");
                      continue;
                  }else if(j==1){ // Get duration from the list
                      duration = (String)point.get("duration");
                      continue;
                  }
  
                  double lat = Double.parseDouble(point.get("lat"));
                  double lng = Double.parseDouble(point.get("lng"));
                  LatLng position = new LatLng(lat, lng);
  
                  points.add(position);
              }
  
              // Adding all the points in the route to LineOptions
              lineOptions.addAll(points);
              lineOptions.width(2);
              lineOptions.color(Color.RED);
          }
  
          tvDistanceDuration.setText("Distance:"+distance + ", Duration:"+duration);
  
          // Drawing polyline in the Google Map for the i-th route
          map.addPolyline(lineOptions);
      }
  }
  </code></pre>
  
  <p>My assumption went wrong.Here the distance and duration are retrieved for every leg(they are not even stored) and after looping through every route and leg ,finally the textview is set to those variables(distance and duration) and it gives the overall distance and duration.</p>
  
  <p><strong><em>So my question is how the overall distance and duration are computed without any manual mathematical operations,while we have parsed those objects corresponding to all individual legs?</em></strong>  </p>