2016-11-11 28 views
0

我是一個android noob,我正在探索android地圖api.I指的是這個site。 我可以理解除距離和持續時間檢索以外的所有內容。 JSON解析代碼如下。在Android谷歌地圖方向API實際計算的距離和持續時間?

public List<List<HashMap<String,String>>> parse(JSONObject jObject){ 

    List<List<HashMap<String, String>>> routes = new  ArrayList<List<HashMap<String,String>>>() ; 
    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<jRoutes.length();i++){ 
      jLegs = ((JSONObject)jRoutes.get(i)).getJSONArray("legs"); 

      List<HashMap<String, String>> path = new ArrayList<HashMap<String, String>>(); 

      /** Traversing all legs */ 
      for(int j=0;j<jLegs.length();j++){ 

       /** Getting distance from the json data */ 
       jDistance = ((JSONObject) jLegs.get(j)).getJSONObject("distance"); 
       HashMap<String, String> hmDistance = new HashMap<String, String>(); 
       hmDistance.put("distance", jDistance.getString("text")); 

       /** Getting duration from the json data */ 
       jDuration = ((JSONObject) jLegs.get(j)).getJSONObject("duration"); 
       HashMap<String, String> hmDuration = new HashMap<String, String>(); 
       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<jSteps.length();k++){ 
        String polyline = ""; 
        polyline = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("polyline")).get("points"); 
        List<LatLng> list = decodePoly(polyline); 

        /** Traversing all points */ 
        for(int l=0;l<list.size();l++){ 
         HashMap<String, String> hm = new HashMap<String, String>(); 
         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; 
} 

他們獲得的距離和持續時間數據爲每一個leg.The Android的文檔稱,距離表示這個航段「期間的總距離表示該腿的總持續時間「。因此,我的假設是,在最終距離和持續時間計算期間,所有這些數據都將被添加。

這是最後的代碼:

protected void onPostExecute(List<List<HashMap<String, String>>> result) { 
     ArrayList<LatLng> points = null; 
     PolylineOptions lineOptions = null; 
     MarkerOptions markerOptions = new MarkerOptions(); 
     String distance = ""; 
     String duration = ""; 

     if(result.size()<1){ 
      Toast.makeText(getBaseContext(), "No Points", Toast.LENGTH_SHORT).show(); 
      return; 
     } 

     // Traversing through all the routes 
     for(int i=0;i<result.size();i++){ 
      points = new ArrayList<LatLng>(); 
      lineOptions = new PolylineOptions(); 

      // Fetching i-th route 
      List<HashMap<String, String>> path = result.get(i); 

      // Fetching all the points in i-th route 
      for(int j=0;j<path.size();j++){ 
       HashMap<String,String> 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); 
    } 
} 

我的假設去wrong.Here的距離和持續時間爲每腿檢索(他們甚至不存儲),並通過各種途徑和腿部,最後循環後textview被設置爲這些變量(距離和持續時間),它給出了整體距離和持續時間。

所以我的問題是如何計算總距離和持續時間沒有任何手動數學運算,而我們已經解析了所有單個腿對應的對象?

回答

相關問題