MapImageLayer - Set definition expressions on sublayers

This sample shows how to add an instance of MapImageLayer to a Map in a MapView and update thedefinitionExpression of a sublayer.

Use dark colors for code blocksCopy
                                             
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// This layer has four sublayers. You can define the definitionExpression in each sublayer.
const layer = new MapImageLayer({
  url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer",
  sublayers: [
    {
      id: 3,
      visible: false
    },
    {
      id: 2,
      visible: true
    },
    {
      id: 1,
      visible: true
    },
    {
      id: 0,
      visible: true,
      definitionExpression: "pop2000 > 100000"
    }
  ]
});

...

/*****************************************************************
 * Listen for events on when the slider values have changed.
 * When the slider value changes, apply the new value to the
 * MapImageLayer definitionExpression.
 *****************************************************************/
layer.when(() => {
  const cities = layer.findSublayerById(0);
  populationSlider.on("thumb-drag", populationValueChanged);

  const populationValueChanged = (event) => {
    selectedPopulation = event.value;
    document.getElementById("population-display").innerHTML = parseInt(selectedPopulation).toLocaleString()
    // update the layers after the user stops the slider to avoid continues requests
    if (event.state === 'stop')
    {
      cities.definitionExpression = "pop2000 > " + selectedPopulation;
    }
  }
});

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.