Hide Table of Contents
Latest Samples
View Feature layer with layer definition applied sample in sandbox
Feature layer with layer definition applied

Description

This sample demonstrates how to use layer definitions with feature layers to limit the information that gets displayed on the map. Tooltips for each feature are also displayed as you mouse over the map. The tooltips are created using dojo's TooltipDialog dijit.

This data contains oil and gas fields for the state of Kansas. In this example layer definitions are used to only display features that meet the specified critiera, in this case features with a value of 'Yes' for the PROD_GAS field.

The following code creates a new feature layer, specifies the out fields and renderer, then applies the definition expression using the featureLayer.setDefinitionExpression method.

featureLayer.setDefinitionExpression("PROD_GAS='Yes'");

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

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>Layer in a map service - [ON-DEMAND]</title>

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

    <script>var dojoConfig = { parseOnLoad:true };</script>
    <script src="https://js.arcgis.com/3.26/"></script>
    <script>
      dojo.require("esri.map");
      dojo.require("esri.layers.FeatureLayer");
      dojo.require("dijit.TooltipDialog");

      var map;

      function init() {
        map = new esri.Map("mapDiv", {
          basemap: "streets",
          center: [-97.395, 37.537],
          zoom: 12
        });

        var featureLayer = new esri.layers.FeatureLayer("https://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSPetro/MapServer/1",{
          mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
          outFields: ["*"],
          opacity: 0.5
        });

        featureLayer.on("mouse-over", showTooltip);
        featureLayer.on("mouse-out", closeDialog);

        featureLayer.setDefinitionExpression("PROD_GAS='Yes'");
        map.addLayer(featureLayer);
      }

      function showTooltip(evt){
        closeDialog();
        var tipContent = "<b>Status</b>: " + evt.graphic.attributes.STATUS +
          "<br><b>Cummulative Gas</b>: " + evt.graphic.attributes.CUMM_GAS + " MCF" +
          "<br><b>Total Acres</b>: " +  evt.graphic.attributes.APPROXACRE +
          "<br><b>Avg. Field Depth</b>: " + evt.graphic.attributes.AVGDEPTH + " meters";

        var dialog = new dijit.TooltipDialog({
          id: "tooltipDialog",
          content: tipContent,
          style: "position: absolute; width: 250px; font: normal normal bold 6pt Tahoma;z-index:100"
        });
        dialog.startup();

        dojo.style(dialog.domNode, "opacity", 0.85);
        dijit.placeOnScreen(dialog.domNode, {x: evt.pageX, y: evt.pageY}, ["TL", "BL"], {x: 10, y: 10});
      }

      function closeDialog() {
        var widget = dijit.byId("tooltipDialog");
        if (widget) {
          widget.destroy();
        }
      }

    dojo.ready(init);
  </script>
</head>

<body class="soria">
  <div id="mapDiv"></div>
</body>

</html>
 
          
Show Modal