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 }); });
Solved! Go to Solution.
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"
inputFeatures.features = result; console.log(inputFeatures.features);
The features are retrieved once the feature layer is added to the map. After the onUpdateEnd event has fired
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
What can I do if I dont want to add this layer to the map?
var map = new Map("mapDiv"); map.addLayer(school);
How can I retrieve its graphics anyway?
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?
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 })
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) }); });
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"
inputFeatures.features = result; console.log(inputFeatures.features);