How to list all polygons in the feature layer?

1319
3
Jump to solution
02-06-2019 09:16 PM
Valgenmap
New Contributor III

arcgis‌

I have added FeatureLayer as below into the map,

var map = new Map("map", {
basemap: "streets-navigation-vector",
showLabels:false,
showAttribution: false,
slider: false,
center: [-122.45, 37.75],
zoom: 10,
minZoom: 2,
});

var selectedBoundaryServiceUrl = "https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/3";

var stateLayer = new esri.layers.FeatureLayer(selectedBoundaryServiceUrl, {
id: "Selected_Layer",
opacity: 0.7,
mode: esri.layers.FeatureLayer.MODE_AUTO,
outFields: ["*"],
infoTemplate: infoTemplate
});

g_esri.map.addLayer(stateLayer);

I tried below line of code to get array of graphics in the layer.

var listOfGraphics = map.getLayer("Selected_Layer").graphics

But the output I'm getting as "Zero"

Here I'm adding the console output for reference.

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

You have to wait until the map is loaded before attempting to access its graphics. If you use "map.addLayer", then you'll have to use syntax to check when the specific layer is loaded (since the event fires each time a layer is loaded, including basemap layers)

    map.addLayer(featureLayer);
    map.on('layer-add-result', function(addLayer){
      if (addLayer.layer === featureLayer){
        console.log(featureLayer.graphics);
      }
    });‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Or you could use "map.addLayers", which fires when all layers are loaded.

    map.addLayers([featureLayer]);
    map.on('layers-add-result', function (){
      console.log(featureLayer.graphics);
    });‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

3 Replies
KenBuja
MVP Esteemed Contributor

You have to wait until the map is loaded before attempting to access its graphics. If you use "map.addLayer", then you'll have to use syntax to check when the specific layer is loaded (since the event fires each time a layer is loaded, including basemap layers)

    map.addLayer(featureLayer);
    map.on('layer-add-result', function(addLayer){
      if (addLayer.layer === featureLayer){
        console.log(featureLayer.graphics);
      }
    });‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Or you could use "map.addLayers", which fires when all layers are loaded.

    map.addLayers([featureLayer]);
    map.on('layers-add-result', function (){
      console.log(featureLayer.graphics);
    });‍‍‍‍‍‍‍‍‍‍‍‍
Valgenmap
New Contributor III

Is it possible to get the list of features before adding it on Map? 

By using some AJAX or something.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Valgen,

  Use esriRequest to make a service call to the rest endpoint for the MapService or FeatureService, with a query where of 1=1.

Here is a sample that shows that:

Request layer info | ArcGIS API for JavaScript 3.27 

0 Kudos