Select to view content in your preferred language

closestFacility question trouble with query tasks and graphics

1140
6
Jump to solution
09-20-2012 01:31 PM
Charlesshultz
Regular Contributor
I have been using the API for a little bit but am really confused about results from query tasks and graphics layers...

What I am trying to do I thought would be simple but...I'm stuck. I am working with the closestFacility example for my application and from what I understand it can only use graphics... That said, the way I am doing it is this...

1) Add 50 or so facility graphics using a query task on a feature service. I already have a feature layer from this layer but I need graphics for the closestFacility task.
2) Get the XY of the feature the user clicked on and turn that into a graphic
3)well lets stop here because I do not have 1 or 2 working...

The main issue I am having is the facility graphics that I define in the initialization function comes up undefined when I try to use it.

var YardGraphicsLayer = new esri.layers.GraphicsLayer();     YardGraphicsLayer.id = 'YardGraphic';     map.addLayer(YardGraphicsLayer);      var Yardsymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 40,             new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,             new dojo.Color([255, 0, 0]), 4),             new dojo.Color([0, 255, 0, 0.25]));      queryYards = new esri.tasks.Query();     queryYards.returnGeometry = true;     queryYards.outFields = ["NAME_CITY", "REGION"];     queryYards.where = "1=1";     queryTaskYards = new esri.tasks.QueryTask("http://services.arcgis.com/s94UJcOoEa23NHUs/arcgis/rest/services/Rigs_by_Yards/FeatureServer/0");     queryTaskYards.execute(queryYards, function (results) {          var featureSet = results.features;         alert("Yards: " + featureSet.length);          for (var i = 0, il = featureSet.length; i < il; i++) {             var graphic = featureSet;             graphic.setSymbol(Yardsymbol);             YardGraphicsLayer.add(graphic);         }     }, errCallback);



If I try map.getLayer("YardGraphicsLayer");  when I want to use the layer I get an undefined..
To put it easy I would like to use my own feature layers in the closest Facility example...
Any help would be great,
Charles
0 Kudos
1 Solution

Accepted Solutions
derekswingley1
Deactivated User
What does your call to closestFacilityTask.solve() look like?

The code you posted looks like it should work. There's also the Find Closest Facilities sample that you can use as an example.

Also confirm that map.getLayer("featureLayerYard") returns your feature layer.

View solution in original post

0 Kudos
6 Replies
derekswingley1
Deactivated User
Before getting too deep, one thing to establish is that features in a feature layer are graphics. That is, featureLayer.graphics is an array of esri.Graphic objects, the same way that graphicsLayer.graphics is an array of esri.Graphic objects.

Also, you should specify an ID for a layer when you create it. Instead of:
var YardGraphicsLayer = new esri.layers.GraphicsLayer();
YardGraphicsLayer.id = 'YardGraphic';


Try:
var YardGraphicsLayer = new esri.layers.GraphicsLayer({ "id": "YardGraphic" });


Finally, when a graphic is clicked, a reference to that graphic is available as a property on the event object. For instance, to access a graphic that is clicked, do this:
dojo.connect(graphicsLayer, "onClick", function(evt) {
  console.log("clicked graphic: ", evt.graphic);
});
0 Kudos
Charlesshultz
Regular Contributor
Thank you for the reply.. It looks like I did not need to do all that work when I already had a featurelayer which is a graphics layer..

So how do I set

  var facilities = new esri.tasks.FeatureSet();
    facilities.features =

to my featurelayer? I'm sure this is a simple answer...

function ClosestYard() {

    var facilities = new esri.tasks.FeatureSet();
    facilities.features = map.getLayer("featureLayerYard").graphics;  /// This is wrong way to put featurelayer into
    

    params.facilities = facilities;
    params.outSpatialReference = map.spatialReference;

    closestFacilityTask = new esri.tasks.ClosestFacilityTask("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Network/USA/NAServer/Closest Facility");
........
0 Kudos
JohnGravois
Deactivated User
featureLayers provide you with some nifty tools for setting a definition expression to limit whats drawn, but if you are already handing on to an array of graphics attempting to pass them to a featureLayer isn't really going to make your life any easier.

derek is definitely right that the results you have are already graphics.  you just have to remember that even though the graphics you get back when querying a layer have geometry and attributes, they don't have symbology defined yet. 

in the code snippet below we loop through the results and assign them to a new variable called graphic before adding a symbol and pushing to the map

dojo.forEach(featureSet.features,function(feature){
          var graphic = feature;
          graphic.setSymbol(symbol);

          //Set the infoTemplate.
          graphic.setInfoTemplate(infoTemplate);

          //Add graphic to the map graphics layer.
          map.graphics.add(graphic);
        
        });


but you could also skip defining the new variable name graphic and do this directly to "feature"

dojo.forEach(featureSet.features,function(feature){
          //var graphic = feature;
          feature.setSymbol(symbol);

          //Set the infoTemplate.
          feature.setInfoTemplate(infoTemplate);

          //Add graphic to the map graphics layer.
          map.graphics.add(feature);
        
        });


http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples/query_clickinfowindow.html

does that clear things up any? 🙂
0 Kudos
Charlesshultz
Regular Contributor
I think I am not understanding because I have an existing feature layer on the map and I want to assign that feature layer to the facility parameter of the geoprocessing task. That parameter takes a graphics layer which a feature layer is so my confusion come from assigning that graphic collection of the existing feature layer to the param.
function ClosestYard() {

    var facilities = new esri.tasks.FeatureSet();
    facilities.features = map.getLayer("featureLayerYard").graphics;  /// This is wrong way to put featurelayer into Params
   

    params.facilities = facilities;
    params.outSpatialReference = map.spatialReference;

    closestFacilityTask = new esri.tasks.ClosestFacilityTask("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Network/USA/NAServer/Closest Facility");
........


I am missing the steps and not understanding how to get from existing feature layer on the map to setting that layer as the Params.facilities
Thanks again...
Charles
0 Kudos
derekswingley1
Deactivated User
What does your call to closestFacilityTask.solve() look like?

The code you posted looks like it should work. There's also the Find Closest Facilities sample that you can use as an example.

Also confirm that map.getLayer("featureLayerYard") returns your feature layer.
0 Kudos
Charlesshultz
Regular Contributor
Thank you very much for the reply...
I think I know what the issue was... I will test tomorrow. I don't think I gave the feature layer an id.
I think I was using the feature layer var name. If that is the case I will kick myself in the butt on your behalf.
I'll see if that gets it working and post if I have any other issues with the task occur.
Thanks again,
Charles
0 Kudos