Question

i've downloaded a project named RoutingSample from arcgis, then i tried to use it on my eclipse. it's running on my tablet without errors, but when it arrive to the command "RouteTask.solve(rp);" it raises an exception so it will not be able to calculate the route :

'com.esri.core.io.EsriServiceException: Unable to complete operation.

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.'

any ideas ?

this is my location first :

public void onLocationChanged(Location loc) {
        if (loc == null)
            return;
        boolean zoomToMe = (mLocation == null) ? true : false;
        mLocation = new Point(loc.getLongitude(), loc.getLatitude());
        if (zoomToMe) {
        //  Point mapPoint = (Point) GeometryEngine.project(mLocation, SpatialReference.create(4326), map.getSpatialReference());
            Point p = (Point) GeometryEngine.project(mLocation, egs, wm);
            map.zoomToResolution(p, 20.0);
        }
    }

and this is the on long pess listener that i calculate the route in :

            // Clear the graphics and empty the directions list
            routeLayer.removeAll();
            hiddenSegmentsLayer.removeAll();
            curDirections = new ArrayList<String>();
            mResults = null;

            // retrieve the user clicked location
            final Point loc = map.toMapPoint(x, y);

            // Show that the route is calculating
            dialog = ProgressDialog.show(RoutingSample.this, "",
                    "Calculating route...", true);
            // Spawn the request off in a new thread to keep UI responsive
            Thread t = new Thread() {
                @Override
                public void run() {
                    try {
                        // Start building up routing parameters
                        RouteParameters rp = mRouteTask
                                .retrieveDefaultRouteTaskParameters();
                        NAFeaturesAsFeature rfaf = new NAFeaturesAsFeature();
                        // Convert point to EGS (decimal degrees)
                        Point p = (Point) GeometryEngine.project(loc, wm,
                                egs);
                    
                        // Create the stop points (start at our location, go
                        // to pressed location)
                        
                        StopGraphic point1 = new StopGraphic(mLocation);
                        StopGraphic point2 = new StopGraphic(p);
                        rfaf.setFeatures(new Graphic[] { point1, point2 });
                        rfaf.setCompressedRequest(true);
                        rp.setStops(rfaf);
                        // Set the routing service output SR to our map
                        // service's SR
                        rp.setOutSpatialReference(wm);

                        // Solve the route and use the results to update UI
                        // when received
                        mResults = mRouteTask.solve(rp);
                        mHandler.post(mUpdateResults);
                    } catch (Exception e) {
                        mException = e;
                        mHandler.post(mUpdateResults);
                    }
                }
            };
            // Start the operation
            t.start();
            return true;
Was it helpful?

Solution

I had the same exception, the solution was to review the SpatialReference of service and application, use the same in both, if the service has a different SpatialReference then you will find no location. eg, uses for GPS 4326.

I hope it helps.

Example:

if you declare SpatialReference at

private SpatialReference wm,egs;
......
wm = SpatialReference.create(102100);
egs = SpatialReference.create(4326);

then make sure your MapService SpatialReference is:

Spatial Reference: 102100  (4326)

(If you are using Esri MapService and NAService services in the Routing example https://developers.arcgis.com/android/sample-code/routing/ do not need to worry about that)

Now you can convert the screen point in onLongPressListener

Point loc = map.toMapPoint(x, y);
Point p = (Point) GeometryEngine.project(loc, wm, egs);

/*initialize RouteTask*/
RouteTask rt = RouteTask.createOnlineRouteTask("your service url",null);
RouteParameters rp = rt.retrieveDefaultRouteTaskParameters();
NAFeaturesAsFeature rfaf = new NAFeaturesAsFeature();

//mlocation is your latitude and longitude point provided by GPS location in decimal degrees
StopGraphic point1 = new StopGraphic(mlocation);                            
StopGraphic point2 = new StopGraphic(p);
rfaf.setFeatures(new Graphic[]{point1,point2});

rfaf.setCompressedRequest(true);
rp.setStops(rfaf);
rp.setOutSpatialReference(wm);

RouteResult mresults = rt.solve(rp);

It works fine for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top