[GIS] Finding shortest route between multiple stops using ArcGIS for Android

arcgis-androidroutingshortest path

ArcGIS is already equipped with API service which calculates the shortest path between a start and an end point based on length, time etc.
The following is how the service is called to returns in a RouteResult object, which in turn is used to draw a route on the map (I omit that part):

try {
            NAFeaturesAsFeature rfaf = new NAFeaturesAsFeature();
            // Create the stop points (start at our location, go to pressed location)
            ArrayList<StopGraphic> stopGraphics = new ArrayList<StopGraphic>();
            for(Point pt : directionStops){
                stopGraphics.add(new StopGraphic(pt));
            }
            Graphic[] stopGraphicsPrim = new Graphic[stopGraphics.size()];
            stopGraphicsPrim = stopGraphics.toArray(stopGraphicsPrim);
            rfaf.setFeatures(stopGraphicsPrim);
            rfaf.setCompressedRequest(true);
            A.getRouteParameter().setStops(rfaf);
            //A.getRouteParameter().setImpedanceAttributeName("length");
            // Set the routing service output SR to our map service's SR
            A.getRouteParameter().setOutSpatialReference(wm);
            mResults = A.getRouteTask().solve(A.getRouteParameter());
        } catch (Exception e) {
            e.printStackTrace();
            dialogRouting.dismiss();
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getActivity(),"directions cannot be found ",Toast.LENGTH_SHORT).show();
                    hideSoftKeyboard(getActivity());
                }
            });
        }

I now have multiple destination points. Say, someone needs to visit 5 coordinates from its own location, but, he/she needs to follow a path which ends up in a shortest travel distance, having visited all the 5 destinations.

I could not find a service from ArcGIS server which provides me such a general path.
If I just provide RouteParameter object with multiple stops with method: A.getRouteParameter().setStops(rfaf); the service will return a path in the given coordinate order. i.e. if my first stop coordinate is the farthest to the start coordinate (own current coordinate in my case), the the route first points to there instead of finding the closest one from the all given stops.

Now, are there any ways to find this shortest general path using any service (maybe apart from ArcGIS), or do I have to make multiple service calls between two points to eventually calculate the shortest general path?
i.e. for N multiple stops I have to make Nx(N+1)/2 point to point service calls with the method: mResults = A.getRouteTask().solve(A.getRouteParameter());, as mentioned in the above code snippet.

Best Answer

The findBestSequence parameter on the RouteParameters class should re-order the stops in the most efficient order (depending on whether you want shortest or fastest route), from the API help :

Find Best Sequence

Gets or sets a value indicating whether the analysis should reorder stops to find the optimized route. The default is false.

Note using this you may wish to preserve the first and/or last stops if that's an origin and destination of the route, e.g.

A.getRouteParameter().setFindBestSequence(true);
A.getRouteParameter().setPreserveFirstStop(true);
Related Question