Select to view content in your preferred language

FeatureLayer.grahics is empty

1493
5
Jump to solution
01-16-2014 04:07 AM
MaxDemars
Deactivated User
Hi,

I am new to the javascript API and I dont know what is going wrong in my simple map application. I have a map servcie published by arcgis Server and I would like to select a layer of this service to use in a geoprocessing tool. However, I cant retrieve the graphics of this layer.

I am doing like this:

     require(["esri/map", "dojo/domReady!", "esri/layers/ArcGISDynamicMapServiceLayer", "esri/layers/FeatureLayer", "esri/tasks/Geoprocessor", "esri/SpatialReference",], function(Map) {  /*initializing map, etc... */  var school = new esri.layers.FeatureLayer("http://localhost:6080/arcgis/rest/services/carte1/MapServer/0");    //trying to retieve the graphics with load event because the map service is asynchronous            school.on("load", function(){      inputFeatures.features = school.graphics;      inputFeatures.fields = school.dataAttributes;      alert(inputFeatures.features) //the alert box is empty     }); });



Why can't I get the graphics like this?

Thank you,
0 Kudos
1 Solution

Accepted Solutions
JohnathanBarclay
Regular Contributor
var query = new Query(); query.where = "1=1"; school.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(result){   console.log(result);//This is an array of graphics })


Still cant get the graphics from the selection I do like this:

      function setupParameters(){          var inputFeatures = new esri.tasks.FeatureSet();          var query = new esri.tasks.Query();          query.where = "1=1";          var school = new esri.layers.FeatureLayer("http://localhost:6080/arcgis/rest/services/carte1/MapServer/0",{             mode: esri.layers.FeatureLayer.MODE_SNAPSHOT,           });          map.addLayer(school);          school.on("update-end", function(){          school.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW, function(result){                inputFeatures.features = result.graphics;                console.log(inputFeatures.features)          });         });


console.log returns "undefined"


You were nearly there; the selectFeatures callback returns an array of graphics, so its simply:

inputFeatures.features = result; console.log(inputFeatures.features);

View solution in original post

0 Kudos
5 Replies
JakubMalec
Deactivated User
Documentation says:
The features are retrieved once the feature layer is added to the map. After the onUpdateEnd event has fired


If you want to get features from current extent, you need to initialize the layer in MODE_ONDEMAND mode. If you want to get all features, you need to set the mode to MODE_SNAPSHOT (but it will return no more than the default total of 1000 features).

Read more at: https://developers.arcgis.com/en/javascript/jsapi/featurelayer.html
0 Kudos
MaxDemars
Deactivated User
Documentation says:


If you want to get features from current extent, you need to initialize the layer in MODE_ONDEMAND mode. If you want to get all features, you need to set the mode to MODE_SNAPSHOT (but it will return no more than the default total of 1000 features).

Read more at: https://developers.arcgis.com/en/javascript/jsapi/featurelayer.html


Hi,

Thank you for your reply. What can I do if I dont want to add this layer to the map? How can I retrieve its graphics anyway?

Ok in MODE_SNAPSHOT I think I could retrieve more than 1000 features if I change the default property in ArcGIS Server Manager.
0 Kudos
JohnathanBarclay
Regular Contributor
What can I do if I dont want to add this layer to the map?


First create the map then add the layer as follows:

var map = new Map("mapDiv");
map.addLayer(school);


How can I retrieve its graphics anyway?


Via the FeatureLayer's selectFeatures() method. You will need to define a Query first (require "esri/tasks/query"), but if you want to retrieve all features you can set the Query's where property to "1=1":

var query = new Query();
query.where = "1=1";
school.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(result){
  console.log(result);//This is an array of graphics
})


What? It is impossible to get more than 1000 features? Why this limitation?


No you can change the limit in the service itself:

1. Open ArcCatalog and browse to your service
2. Right click and select Service Properties (service must be stopped first)
3. Select the Parameters tab, then change the "Maximum Number of Records Returned by Server" to your desired amount
0 Kudos
MaxDemars
Deactivated User
var query = new Query();
query.where = "1=1";
school.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(result){
  console.log(result);//This is an array of graphics
})


Still cant get the graphics from the selection I do like this:

      function setupParameters(){
         var inputFeatures = new esri.tasks.FeatureSet();
         var query = new esri.tasks.Query();
         query.where = "1=1";
         var school = new esri.layers.FeatureLayer("http://localhost:6080/arcgis/rest/services/carte1/MapServer/0",{
            mode: esri.layers.FeatureLayer.MODE_SNAPSHOT, 
         });
         map.addLayer(school);
         school.on("update-end", function(){
         school.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW, function(result){
               inputFeatures.features = result.graphics;
               console.log(inputFeatures.features)
         });
        });


console.log returns "undefined"
0 Kudos
JohnathanBarclay
Regular Contributor
var query = new Query(); query.where = "1=1"; school.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(result){   console.log(result);//This is an array of graphics })


Still cant get the graphics from the selection I do like this:

      function setupParameters(){          var inputFeatures = new esri.tasks.FeatureSet();          var query = new esri.tasks.Query();          query.where = "1=1";          var school = new esri.layers.FeatureLayer("http://localhost:6080/arcgis/rest/services/carte1/MapServer/0",{             mode: esri.layers.FeatureLayer.MODE_SNAPSHOT,           });          map.addLayer(school);          school.on("update-end", function(){          school.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW, function(result){                inputFeatures.features = result.graphics;                console.log(inputFeatures.features)          });         });


console.log returns "undefined"


You were nearly there; the selectFeatures callback returns an array of graphics, so its simply:

inputFeatures.features = result; console.log(inputFeatures.features);
0 Kudos