Select to view content in your preferred language

Closest Facility with Feature Layer from ArcGIS Online

1270
4
Jump to solution
05-07-2021 08:49 AM
JohnPaulAlbana
Emerging Contributor

I am trying to use a feature layer from ArcGIS Online to be an input facility in my closest facility parameter. I just recently started using ArcGIS JavaScript API and I saw someone's example in this forum which looks similar to this:

            facilitiesFeatureLayer = new FeatureLayer(
                    mode:FeatureLayer.MODE_SNAPSHOT
                });
            let featuresetFacilities = new FeatureSet({
            });
            featuresetFacilities.features = facilitiesFeatureLayer.graphics;
 
            function findClosestFacility(startFeaturefacilityFeatures) {

                const closestFacilityTask = new ClosestFacilityTask({
                });

                const params = new ClosestFacilityParameters({
                    incidents: new FeatureSet({
                        features: [startFeature],
                    }),
                    returnRoutes: true,
                    returnFacilities: true,
                    defaultTargetFacilityCount: 3,
                });
                params.facilities = featuresetFacilities;

 

The code runs but returns no results:

HEJHh

0 Kudos
1 Solution

Accepted Solutions
JohnGrayson
Esri Regular Contributor

In your codepen you are mixing 3.x and 4.x code; that won't work.  Here's a slightly modified version of your code that gets and sets the facilities before you start trying to call the solver:

https://codepen.io/john-grayson/pen/oNZgPYX

 

View solution in original post

4 Replies
Noah-Sager
Esri Regular Contributor

 Hi @JohnPaulAlbana, can you post a link to an app that reproduces this issue? On something like Codepen would be great. You can omit the apiKey if you are using one. Then I can investigate further.

0 Kudos
JohnPaulAlbana
Emerging Contributor
0 Kudos
Noah-Sager
Esri Regular Contributor

The first error you see when loading the app is: Uncaught (in promise) ReferenceError: facilities is not defined

So we need to get the locations of the features into a facilities variable. There is some good doc to go through here: https://developers.arcgis.com/documentation/mapping-apis-and-services/routing/closest-facility-routi...

I can take a closer look next week as well.

0 Kudos
JohnGrayson
Esri Regular Contributor

In your codepen you are mixing 3.x and 4.x code; that won't work.  Here's a slightly modified version of your code that gets and sets the facilities before you start trying to call the solver:

https://codepen.io/john-grayson/pen/oNZgPYX