距离和持续时间实际上是如何计算在android谷歌地图方向api



我是一个android新手,我正在探索android地图api。我指的是这个网站。我能理解除了距离和时间检索。JSON解析代码如下:

    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;
}

他们正在获取每条腿的距离和持续时间数据。Android文档中说,distance表示这条腿的总距离"duration表示这条腿的总持续时间"。所以我的假设是,在计算最终距离和持续时间时,所有这些数据都将被添加。

这是最终代码:

    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);
    }
}

我的假设错了。这里的距离和持续时间为每条腿检索(他们甚至没有存储),并通过每条路线和腿循环后,最后textview被设置为这些变量(距离和持续时间),它给出了总距离和持续时间。

所以我的问题是,当我们解析了对应于所有单独腿的对象时,如何在没有任何手动数学操作的情况下计算总距离和持续时间?

使用距离矩阵API获取实际距离和时间。请看下面的链接

https://developers.google.com/maps/documentation/distance-matrix/start

最新更新