AnsweredAssumed Answered

Get FeatureLayer Attribute by Clicking

Question asked by xuankuai on Jul 30, 2018
Latest reply on Jul 30, 2018 by rscheitlin

I'm using ArcGIS API for JavaScript 4.8. My goal is to get the Census tract ID value of the clicked Feature in a FeatureLayer and display the ID value in an alert box. I got stuck when trying to use Query/QueryTask to fetch the value. My code goes here:

 

require([
    "esri/Map",
    "esri/views/MapView",
    "esri/layers/FeatureLayer",
    "esri/layers/GroupLayer",
    "esri/widgets/LayerList",
    "esri/tasks/support/Query",
    "esri/tasks/QueryTask",
    "dojo/on",
    "dojo/domReady!"
  ],
  function(
    Map, MapView, FeatureLayer, GroupLayer, LayerList, Query, QueryTask, on
  ) {
    var map = new Map({
      basemap: "topo"
    });

    var view = new MapView({
      center: [-91.05, 30.5161109],
      zoom: 11,
      container: "viewDiv",
      map: map,
    });

    // Generate link to Census tract details
    view.when(function() {
      on(view, "click", displayTractID);
    });

    function displayTractID(event) {
      // TODO
    }

    // Define popup template and get feature layers
    var census2000Template = {
      title: "Census 2000",
      content: [{
        type: "fields",
        fieldInfos: [{
          fieldName: "TRACT",
          label: "Tract",
          visible: true
        }, {
          fieldName: "POPULATION",
          label: "Population",
          visible: true,
          format: {
            digitSeparator: true,
            places: 0
          }
        }]
      }]
    };

    var census2000Layer = new FeatureLayer({
      url: "http://services.arcgis.com/KYvXadMcgf0K1EzK/ArcGIS/rest/services/Census_2000/FeatureServer/0",
      outFields: ["*"],
      popupTemplate: census2000Template
    });

    // Add feature layers to a group layer
    var censusGroupLayer = new GroupLayer({
      title: "Historic Census",
      visible: true,
      visibilityMode: "exclusive",
      layers: [census2000Layer]
    });

    map.add(censusGroupLayer);
  });

 

Could someone help me to fill out the TODO part or show me some sandbox examples?

Outcomes