如何将航点,起点和终点的所有距离和旅行时间相加

时间:2018-02-22 03:02:26

标签: java android

我正在构建一个应用程序,其中显示一个带有航路点的路线,并显示总行程时间和距离,但问题是不是所有航路点的总和,不显示总距离和总时间,只显示我一条腿,我需要总结所有航点和目的地的航点,我该怎么做?

JSON Code

Directionfinder.java 的片段代码下面,我尝试这样做,但它不起作用

private void parseJSon(String data) throws JSONException {
        if (data == null)
            return;

        List<Route> routes = new ArrayList<Route>();
        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 < jsonRoutes.length(); i++) {
            JSONObject jsonRoute = jsonRoutes.getJSONObject(i);
            Route route = new Route();

            JSONObject overview_polylineJson = jsonRoute.getJSONObject("overview_polyline");
            JSONArray jsonLegs = jsonRoute.getJSONArray("legs");
           // JSONObject jsonLeg = jsonLegs.getJSONObject(0);

           // JSONObject jsonEndLocation = jsonLeg.getJSONObject("end_location");
          //  JSONObject jsonStartLocation = jsonLeg.getJSONObject("start_location");



            for (int j = 0; j < 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.distance = new Distance(jsonDistance.getString("text"), jsonDistance.getInt("value"));
            //route.duration = new Duration(jsonDuration.getString("text"), jsonDuration.getInt("value"));
          //  route.endAddress = jsonLeg.getString("end_address");
            //route.startAddress = jsonLeg.getString("start_address");
            //route.startLocation = new LatLng(jsonStartLocation.getDouble("lat"), jsonStartLocation.getDouble("lng"));
            //route.endLocation = new LatLng(jsonEndLocation.getDouble("lat"), jsonEndLocation.getDouble("lng"));
            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);
    }

1 个答案:

答案 0 :(得分:0)

是的,您必须使用计算的距离和时间值,同时将其添加到路线中,如

for (int i = 0; i < jsonRoutes.length(); i++) {
            JSONObject jsonRoute = jsonRoutes.getJSONObject(i);
            Route route = new Route();

            JSONObject overview_polylineJson = jsonRoute.getJSONObject("overview_polyline");
            JSONArray jsonLegs = jsonRoute.getJSONArray("legs");
           // JSONObject jsonLeg = jsonLegs.getJSONObject(0);

           // JSONObject jsonEndLocation = jsonLeg.getJSONObject("end_location");
          //  JSONObject jsonStartLocation = jsonLeg.getJSONObject("start_location");

           totalDistance = 0;
           totalSeconds = 0; //<-- reset for each loop

            for (int j = 0; j < 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"));
            }
             //adding the calculated values
            route.distance = new Distance("totalDistance",totalDistance);
            route.duration = new Duration("Total Time",totalSeconds); 

            route.points = decodePolyLine(overview_polylineJson.getString("points"));

            routes.add(route);
        }