In ArcGIS Javascript, Map service is a group of layers that are all related to each other. Usually, this service is rendered as a single image (typically on a server) and displayed on the map. In the ArcGIS Javascript web development as per our need, we restrict feature display on the map from this service.

This article will help you to use layer definitions to limit the information from a layer that gets displayed on the map.

What is layer definition in ArcGis Javascript?

Layer definitions are SQL expressions that limit the data shown on the map from the map service. They are used to only display features that meet the specified criteria.

How to use layer definitions on map service?

For better understanding and implementation to use layer definitions over a map look at ESRI_Census_USA service which is used in this map.

ESRI_Census_USA map service looks like. http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer

This service presents various population statistics from Census 2000, including total population, population density, racial counts, and more. The map service presents statistics at the state, county, block group, and block point levels. This is a sample service hosted by ESRI, powered by ArcGIS Server.

When you add this dynamic map service layer to the map state, county, block group, and block point levels in the map are visible because they are grouped to each other and rendered as a single image to map.

Dynamic Map Service

Now to set the visibility of the layers to states and counties add these lines of code

dynamicMapServiceLayer.setVisibleLayers([5,4,3]);

Code example given below, the layer definitions limit the states to Kansas and the counties to those in Kansas with a population over 25000.

var layerDefs = [];
layerDefs[5] = "STATE_NAME='Kansas'";
layerDefs[4] = "STATE_NAME='Kansas' and POP2007>25000";
layerDefs[3] = "STATE_NAME='Kansas' and POP2007>25000";
dynamicMapServiceLayer.setLayerDefinitions(layerDefs);
Map View After Use Layer Definations on Map Service

The following code adds layer definitions to an array, then applies the definitions using the ArcGISDynamicMapServiceLayer.setLayerDefinitions() method. Notice that in the array, the index of a definition matches its corresponding layer index in the map.

If you’re new to building SQL expressions, a helpful resource is About building an SQL expression in the ArcGIS Desktop Help.

Sample Code

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>


<meta name="viewport"
content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>Create Map with Custom ArcGISDynamicMapServiceLayer Layer
Definitions</title>

<link rel="stylesheet"
href="https://js.arcgis.com/3.27/esri/css/esri.css"/>
<style>
html, body, #mapDiv {
padding: 0;
margin: 0;
height: 100%;
}

</style>

<script src="https://js.arcgis.com/3.27/"></script>
<script>
var map;

require([
"esri/map",
"esri/layers/ArcGISDynamicMapServiceLayer",
"esri/layers/ImageParameters",
"dojo/domReady!"
],
function (Map, ArcGISDynamicMapServiceLayer, ImageParameters) {

map = new Map("mapDiv", {
basemap: "streets",
center: [-98.258, 38.236],
zoom: 7
});

//Use the ImageParameters to set map service layer definitions and map service visible layers before adding to the client map.
var imageParameters = new ImageParameters();

//ImageParameters.layerDefinitions takes an array. The index of the array corresponds to the layer id.
//In the sample below an element is added in the array at 3, 4, and 5 indexes.
//Those array elements correspond to the layer id within the remote ArcGISDynamicMapServiceLayer
var layerDefs = [];
layerDefs[5] = "STATE_NAME='Kansas'";
layerDefs[4] = "STATE_NAME='Kansas' and POP2007>25000";
layerDefs[3] = "STATE_NAME='Kansas' and POP2007>25000";
imageParameters.layerDefinitions = layerDefs;

//I want layers 5,4, and 3 to be visible
imageParameters.layerIds = [5, 4, 3];
imageParameters.layerOption = ImageParameters.LAYER_OPTION_SHOW;
imageParameters.transparent = true;

//construct ArcGISDynamicMapServiceLayer with imageParameters from above
var dynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer("https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer",
{"imageParameters": imageParameters});

map.addLayer(dynamicMapServiceLayer);
});
</script>
</head>

<body>
<div id="mapDiv"></div>
</body>
</html>