0

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:

  1. The routes and directions 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 server

    var route = solveResult.routes[0],
        fid = route.attributes.FacilityID,
        facility = facilities.features[fid-1];
    
  2. 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';
    
1

The facilities on the map is a featureLayer that comes with graphics that have information you dont need, like the attributes and symbology. You will want to make new graphics based on the graphics in the featureLayer.

Replace this line

facilities.features = lay.graphics; 

with this

// require the dojo/_base/array module, named arrayUtils here
arrayUtils.forEach(lay.graphics, function(g){
  facilities.features.push(new Graphic(g.geometry));
});
  • Thanks for the answer @Aamir, it does remove the messages about the undefined properties and that's a welcome addition. Any idea about the points mentioned in my question? – nikoshr Jan 26 '15 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.