Select to view content in your preferred language

How to use a Closest Facilities on Android v.100.1.0

943
6
07-14-2017 04:37 AM
VictorGallego1
Deactivated User

I am testing the sample code of ClosestFacilityTask| arcgis-android and I can not make it work. I'd appreciate your help. For example:

// create ClosestFacilityTask instances
 mClosestFacilityTask
= new ClosestFacilityTask(context, "http://www.yourserver.com/arcgis/rest/services/NetworkAnalysis/CA/NAServer/Closest%20Facility");
 
.....

 
// get default parameters
 
final ListenableFuture paramsFuture = sClosestFacilityTask.createDefaultParametersAsync(); (I guess it's a syntax error)
 paramsFeature
.addDoneListener(new Runnable() {
   
@Override
   
public void run() {
     
try {
       mClosestFacilityParameters
= paramsFuture.get(); (Return Object [] when mClosestFacilityParameters is a ListenableFuture?)

       
// add your Facility
       
List facilities = mClosestFacilityParameters.getFacilities(); (Cannot resolve method)
       facilities
.add(new Facility(new Point(-13045663.744, 4039204.580, WEB_MERCATOR)));

       
// add your Incident
       
List incidents = mClosestFacilityParameters.getIncidents(); (Cannot resolve method)
       incidents
.add(new Incident(new Point(-13052490.008, 4035542.739, WEB_MERCATOR)));

       
// solve
       
final ListenableFuture resultFuture = mClosestFacilityTask.solveClosestFacilityAsync(mClosestFacilityParameters);
       resultFuture
.addDoneListener(new Runnable() {
         
@Override
         
public void run() {
           
try {
             
// get ClosestFacilityRoute by facility and incident indexes
             
ClosestFacilityResult result = resultFuture.get(); (Gives an error because it requires a ClosestFacilityResult and get () returns an Object)
             mClosestFacilityRoute
= result.getRoute(0, 0);
           
} catch (Throwable e) {
             
Log.e(e);
           
}
         
}
       
});
     
} catch (Throwable e) {
       
Log.e(e);
     
}
   
}
 
});

Thanks!!

0 Kudos
6 Replies
AlexanderNohe1
Honored Contributor
private void closestFacilities() {
        mClosestFacilityTask = new ClosestFacilityTask(getApplicationContext(), "http://www.yourserver.com/arcgis/rest/services/NetworkAnalysis/CA/NAServer/Closest%20Facility");
        final SpatialReference WEB_MERCATOR = SpatialReferences.getWebMercator();

        // get default parameters
        final ListenableFuture<ClosestFacilityParameters> paramsFuture = mClosestFacilityTask.createDefaultParametersAsync();
        paramsFuture.addDoneListener(new Runnable() {
            @Override
            public void run() {
                try {
                    mClosestFacilityParameters = paramsFuture.get();

                    // add your Facility
                    List facilities = new ArrayList<Facility>();
                    facilities.add(new Facility(new Point(-13045663.744, 4039204.580, WEB_MERCATOR)));
                    mClosestFacilityParameters.setFacilities(facilities);

                    // add your Incident
                    List incidents = new ArrayList<Incident>();
                    incidents.add(new Incident(new Point(-13052490.008, 4035542.739, WEB_MERCATOR)));
                    mClosestFacilityParameters.setIncidents(incidents);

                    // solve
                    final ListenableFuture<ClosestFacilityResult> resultFuture = mClosestFacilityTask.solveClosestFacilityAsync(mClosestFacilityParameters);
                    resultFuture.addDoneListener(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                // get ClosestFacilityRoute by facility and incident indexes
                                ClosestFacilityResult result = resultFuture.get();
                                ClosestFacilityRoute mClosestFacilityRoute = result.getRoute(0, 0);
                            } catch (Throwable e) {
                                Log.e("Facility", e.toString());
                            }
                        }
                    });
                } catch (Throwable e) {
                    Log.e("Facility", e.toString());
                }
            }
        });
    }

You will need to use your own network data service but this should be a more concise sample.

I hope this helps!

VictorGallego1
Deactivated User

Thanks for the reply. It has helped me.

I have another question. I have tried to do the example on a ServiceFeatureLayer with several points.

I have replaced:

List facilities = new ArrayList<Facility>();
facilities.add(new Facility(new Point(-13045663.744, 4039204.580, WEB_MERCATOR)));
mClosestFacilityParameters.setFacilities(facilities);

by:
QueryParameters queryParams = new QueryParameters();
queryParams.setWhereClause("1=1");
queryParams.setReturnGeometry(true);
mClosestFacilityParameters.setFacilities(serviceFeatureTableX, queryParams);

When I do the method result.getRoute(0,0) returned NULL.

Any ideas?

Thanks!


0 Kudos
AlexanderNohe1
Honored Contributor

Did you call :

serviceFeatureTableX.loadAsync()

?

I believe the table may need to be loaded before you can query it (not 100% on that)

0 Kudos
VictorGallego1
Deactivated User

It's a little weird. When I make a query that returns one result there is no problem. But if in the where clause I put "1 = 1" to return all results is when it returns null in "result.getRoute(0,0)".

I have tried serviceFeatureTableX.loadAsync() but it does not solve my problem.

Anyway thanks a lot for your quick reply

0 Kudos
AlexanderNohe1
Honored Contributor

Interesting...

When I get more time to test, I will return to this issue.

0 Kudos
AlexanderNohe1
Honored Contributor

How many results are being returned from your query?

0 Kudos