如何计算距离和旅行时间航点 API 方向

Posted

技术标签:

【中文标题】如何计算距离和旅行时间航点 API 方向【英文标题】:How calculate distance and travel time waypoints API Directions 【发布时间】:2018-02-21 20:28:58 【问题描述】:

我尝试构建一个应用程序,计算从起点 + 目的地 + 航点的距离和旅行时间,但它只显示第一条腿,我需要添加所有腿

这是json:


     "distance" : 
                      "text" : "1.3 km",
                      "value" : 1313
                   ,
                   "duration" : 
                      "text" : "5 min",
                      "value" : 278
                   , 
                   "distance" : 
                      "text" : "1.9 km",
                      "value" : 1854
                   ,
                   "duration" : 
                      "text" : "7 min",
                      "value" : 405
                   ,

我需要添加 1.3km + 1.9km 并且这个结果,是给我返回 3.2km 的值,比如总距离,并且与时间相同旅行。

显示距离和行程时间的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");
        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 jsonDistance = jsonLeg.getJSONObject("distance");
            JSONObject jsonDuration = jsonLeg.getJSONObject("duration");
            JSONObject jsonEndLocation = jsonLeg.getJSONObject("end_location");
            JSONObject jsonStartLocation = jsonLeg.getJSONObject("start_location");


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

        listener.onDirectionFinderSuccess(routes);
    

【问题讨论】:

你的 JSON 好像无效 你能否提供一个完整的有效 JSON 与 routeslegs 属性。 【参考方案1】:

假设这是您的 JSON(我试图从您的提取代码中推断出它)。如果不是您拥有的 JSON,请更正,我会相应地修改我的答案。


  "routes" : [ 
      "overview_polyline": ,
      "legs" : [ 
          "distance" :  "text" : "1.3 km", "value" : 1313 ,
          "duration" :  "text" : "5 min", "value" : 278 
        ,
          "distance" :  "text" : "1.9 km", "value" : 1854 ,
          "duration" :  "text" : "7 min", "value" : 405 
         ]
    ,
      "overview_polyline": ,
      "legs" : [ 
          "distance" :  "text" : "1.4 km", "value" : 1420 ,
          "duration" :  "text" : "6 min", "value" : 305 
        ,
          "distance" :  "text" : "2.1 km", "value" : 2100 ,
          "duration" :  "text" : "9 min", "value" : 540 
         ]
     ]

您需要遍历支路以添加它们并计算路线的总值。为了清楚起见,我删除了所有与距离和持续时间无关的内容。

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

    List<Route> routes = new ArrayList<>();
    JSONObject jsonData = new JSONObject(data);
    JSONArray jsonRoutes = jsonData.getJSONArray("routes");
    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");
        double routeDistanceText = 0d;
        int routeDistanceValue = 0;
        int routeDurationText = 0;
        int routeDurationValue = 0;
        for (int legIndex = 0; legIndex < jsonLegs.length(); legIndex++) 
            JSONObject jsonLeg = jsonLegs.getJSONObject(legIndex);
            JSONObject jsonDistance = jsonLeg.getJSONObject("distance");
            JSONObject jsonDuration = jsonLeg.getJSONObject("duration");

            // extract leg distance text as a double
            String legDistanceTextString = jsonDistance.getString("text");
            double legDistanceText = Double.valueOf(legDistanceTextString.substring(0, legDistanceTextString.indexOf(" ")));

            // extract leg distance value as an integer
            int legDistanceValue = jsonDistance.getInt("value");

            // extract leg duration text as an integer
            String legDurationTextString = jsonDuration.getString("text");
            int legDurationText = Integer.valueOf(legDurationTextString.substring(0, legDurationTextString.indexOf(" ")));

            // extract leg duration value as an integer
            int legDurationValue = jsonDuration.getInt("value");

            // add leg values to route values
            routeDistanceText += legDistanceText;
            routeDistanceValue += legDistanceValue;
            routeDurationText += legDurationText;
            routeDurationValue += legDurationValue;
        

        // add back km and min to distance and duration texts
        String routeDistanceTextString = routeDistanceText + " km";
        String routeDurationTextString = routeDurationText + " min";

        Log.i("TAG", "route: "+i);
        Log.i("TAG", "routeDistanceText: "+routeDistanceTextString);
        Log.i("TAG", "routeDistanceValue: "+routeDistanceValue);
        Log.i("TAG", "routeDurationText: "+routeDurationTextString);
        Log.i("TAG", "routeDurationValue: "+routeDurationValue);

        route.distance = new Distance(routeDistanceTextString, routeDistanceValue);
        route.duration = new Duration(routeDurationTextString, routeDurationValue);

        routes.add(route);
    

这个输出:

route: 0
routeDistanceText: 3.2 km
routeDistanceValue: 3167
routeDurationText: 12 min
routeDurationValue: 683
route: 1
routeDistanceText: 3.5 km
routeDistanceValue: 3520
routeDurationText: 15 min
routeDurationValue: 845

【讨论】:

以上是关于如何计算距离和旅行时间航点 API 方向的主要内容,如果未能解决你的问题,请参考以下文章

Google Maps Api v3:如何更改方向(设置)面板中的默认航点标记?

markdown 旅行时间和方向API

沿路线添加标记

Google Maps JavaScript API v3 方向功能

谷歌地图方向服务航路点超过 50 个

如何在颤动中获得估计的旅行时间和距离