Google map v2,每条折线的备用路线

时间:2017-01-29 09:38:07

标签: android google-maps google-maps-android-api-2 google-maps-api-2

我的代码仅提供到单个目的地的替代路线。 我如何为每个目的地提供替代路线?

我使用onMapLongClick

在地图中首先绘制标记

这是我新的替代路线功能:

  public void alternateRoute(){

    //If the displayed polyline of the route to the first destination is not the last alternative
    //polyline, display the next alternative polyline else called the
    // incrementRouteIndexOfNextOrderPolyline() function. Take a look at
    // incrementRouteIndexOfNextOrderPolyline() and look at this example. example: if we have
    // 138 the next count is 139.
    if(!listOfRouteArray.isEmpty()){
        if((listOfIndicesOfCurrentRoutes.get(0) + 1) <
                listOfRouteArray.get(0).length()){

            alternatePolyline(0, listOfIndicesOfCurrentRoutes.get(0) + 1);

        }else {
            incrementRouteIndexOfNextOrderPolyline(0);
        }
    }

}

使用incrementRouteIndexOfNextOrderPolyline

 private void incrementRouteIndexOfNextOrderPolyline(int indexOfPolyline){

        //sets the polyline of the route to a destination to the first polyline of the list of all
        //possible polylines of the route to that destination. Tha is if a digit was 9 set it to 0.
        alternatePolyline(indexOfPolyline, 0);

        //if the route to destination with dealing with is the route to the lastly added destination
        //return. that is if the digit is 9 in 9999 return.
        if((indexOfPolyline + 1) >= polylines.size()){
            return;
        }

        //if the polyline of the next order route to destination is not that last possible alternative
        //polyline for that route, set it to the next possible polyline else increment the index of it's
        // next order polyline. that is if we have 118 and we are dealing with 8 make the next count to
        // be 119 else we have 119 make it to be 120
        if((listOfIndicesOfCurrentRoutes.get(indexOfPolyline + 1) + 1) <
                listOfRouteArray.get(indexOfPolyline + 1).length()){

            alternatePolyline(indexOfPolyline,
                    listOfIndicesOfCurrentRoutes.get(indexOfPolyline + 1) + 1);

        }else {
            incrementRouteIndexOfNextOrderPolyline(indexOfPolyline + 1);
        }

        //This function is recursive a such the incrementation will done recursively. for example if
        //we have 199 the first order 9 will be set to 0 and the second order 9 incremented. The
        // second order 9 in turns will be set to 0 and the third order 1 will be 2 to give 200. But
        //in case of 189 the result will obviously be 190 without a recursive call.
    }

还有:

   private void alternatePolyline(int indexOfRouteToAlternate, int indexOfNewPolyline){

        listOfIndicesOfCurrentRoutes.set(indexOfRouteToAlternate, indexOfNewPolyline);
        try {
            JSONObject routes = listOfRouteArray.get(indexOfRouteToAlternate).getJSONObject(indexOfNewPolyline);
            JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
            String encodedString = overviewPolylines.getString("points");
            List<LatLng> list = decodePoly(encodedString);
            PolylineOptions options = new PolylineOptions().width(10).color(Color.GREEN).geodesic(true);
            for (int z = 0; z < list.size(); z++) {
                LatLng point = list.get(z);
                options.add(point);
            }
            bigline = mMap.addPolyline(options);
            polylines.get(indexOfRouteToAlternate).remove();
            polylines.set(indexOfRouteToAlternate, bigline);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

我的alternateRoute()仅为单个标记/目的地重建1条折线。

我需要重建多条折线,因为我有多个目的地。

我个人可以亲自给你这个项目。

如有任何澄清,请发表评论。

0 个答案:

没有答案