I have created a webmap on which a feature layer is displayed and I'm trying to find the closest facility when the user clicks on the map.
Based on Find closest facilities in the Javascript API reference, I used the data given by map.getLayer().graphics
to initialize my facilities set (http://jsfiddle.net/nikoshr/ob5rxsd7/ for a demo, http://bit.ly/14VEwL3 for the map):
arcgisUtils.createMap("9a33727d54904de7b80687fb0810119a", 'map').then(function (response) {
var map = response.map;
var lay = map.getLayer(map.graphicsLayerIds[3]);
var facilities = new FeatureSet();
facilities.features = lay.graphics;
var params = new ClosestFacilityParameters();
params.travelMode = 5;
params.facilities = facilities;
params.returnIncidents = true;
params.returnRoutes = true;
params.returnDirections = true;
params.outSpatialReference = map.spatialReference;
map.on('click', function(evt) {
var inPoint = new Point(evt.mapPoint.x, evt.mapPoint.y, map.spatialReference);
var location = new Graphic(inPoint);
var incidents = new FeatureSet();
incidents.features = [location];
params.incidents = incidents;
closestFacilityTask = new ClosestFacilityTask("https://route.arcgis.com/arcgis/rest/services/World/ClosestFacility/NAServer/ClosestFacility_World");
closestFacilityTask.solve(params, function(solveResult){
console.log(solveResult);
});
});
});
and I get the a response looking like this
{
declaredClass: "esri.tasks.ClosestFacilitySolveResult",
directions: [{routeId:1, routeName:"Location 1 - Point 1", summary:{...}, ...}],
facilities: null,
incidents: [{geometry:{...}, attributes:{...}, declaredClass: "esri.Graphic", ...}],
messages:[
{type:50, description:"Input field [VISIBLE] was not mapped to a field in the network analysis class Facilities.", declaredClass:"esri.tasks.NAMessage"},
{type:50, description:"Input field [TYPEID] ...", ...},
{type:50, description:"Location Point 3 in Facilities is on a soft-restricted network element.", declaredClass:"esri.tasks.NAMessage"}
],
pointBarriers: null,
polygonBarriers: null,
polylineBarriers: null,
routes: [{geometry:{...}, attributes:{...}, declaredClass:"esri.Graphic", ...}]
}
Onto the questions:
The
routes
anddirections
attributes seem correct, but how can I get the facility used to build them? I currently use this but it relies on kowing what was sent to the servervar route = solveResult.routes[0], fid = route.attributes.FacilityID, facility = facilities.features[fid-1];
Is there a way to pass an url representing the feature layer for the facilities as hinted in the Closest facility service doc, instead of copying them and sending them back as I do now? Something like
var facilities = new FeatureSet(); facilities.url= 'an url describing the features';