Search code examples
androidroutesarcgis

Cannot create route from one point to another ArcGIS Android


I've been trying very hard to create a route between two points(startPoint, endPoint). But i am getting the following error:

Location "Location 1" in "Stops" is unlocated. Location "Location 2" in "Stops" is unlocated. Need at least 2 valid stops. "Stops" does not contain valid input for any route.

I've posted this question on gis.stackexchange.com and geonet.esri.com and didn't get a reply except one which was not helpful.

My Code:

private final String routeTaskURL = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Network/USA/NAServer/Route";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mMapView = (MapView) findViewById(R.id.map);
    mMapView.enableWrapAround(true);
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                UserCredentials userCredentials = new UserCredentials();
                userCredentials.setUserToken(token, clientID);
                RouteTask routeTask = RouteTask.createOnlineRouteTask(routeTaskURL, userCredentials);
                RouteParameters routeParameters = routeTask.retrieveDefaultRouteTaskParameters();
                NAFeaturesAsFeature naFeatures = new NAFeaturesAsFeature();

                Point startPoint = new Point(36.793653, -119.866896);
                Point stopPoint = new Point(36.795488, -119.853345);

                StopGraphic startPnt = new StopGraphic(startPoint);
                StopGraphic stopPnt = new StopGraphic(stopPoint);

                naFeatures.setFeatures(new Graphic[] {startPnt, stopPnt});
                routeParameters.setStops(naFeatures);

                RouteResult mResults = routeTask.solve(routeParameters);
                List<Route> routes = mResults.getRoutes();
                System.out.println(mResults.getRoutes());

                Route mRoute = routes.get(0);
                Geometry geometry = mRoute.getRouteGraphic().getGeometry();
                Graphic symbolGraphic = new Graphic(geometry, new SimpleLineSymbol(Color.BLUE, 3));
                mGraphicsLayer.addGraphic(symbolGraphic);
                System.out.println(mResults.getStops());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
}

I've searched the internet. Many developers were/are facing this problem. I've tried all the solutions but none of them worked. I got routeTaskURL from the ArcGIS Routing Sample app. The link which is given in the documentation of ArcGIS maps gives me the 403 error if i open it in the browser.

Note: "token" and "clientID" is declared in the first step and they both are taken from the ArcGIS developers console where i registered my application.

Any Suggestions?


Solution

  • Your X and Y values are switched. Change to this:

    Point startPoint = new Point(-119.866896, 36.793653);
    Point stopPoint = new Point(-119.853345, 36.795488);
    

    See the Point class documentation to learn that the constructor parameters are (x, y), not (y, x). The route service you're using has a default spatial reference of 4326, which is unprojected longitude and latitude. -119.866896 and -119.853345 are not valid latitude (y) values, but they are valid longitude (x) values.