Hide Table of Contents
View Show map properties sample in sandbox
Show map properties

Description

This sample shows how to read map and layer properties and report the information back to the user.

  • Map Layers - The code loops through each layer in the map and reports the layer ID, visibility, and opacity.
  • Spatial Reference - Reports the well-known ID (WKID) of the map's spatial reference.
  • Scales - Reports information about each scale level in the portlandParcels tiled map service layer. The function uses the ArcGISTiledMapServiceLayer.tileInfo property, which can show you how many scale levels are in the map cache, what scales they are, and what the resolution is at each scale.
  • Map width and height - Reports the map width and height in pixels.

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>ArcGIS JavaScript API Map Layer</title>
    <link rel="stylesheet" href="https://js.arcgis.com/3.29/dijit/themes/tundra/tundra.css">
    <link rel="stylesheet" href="https://js.arcgis.com/3.29/esri/css/esri.css">
    <style> 
      html, body { height: 100%; width: 100%; margin: 0; padding: 0; }  
    </style> 

    <script src="https://js.arcgis.com/3.29/"></script>
    <script>
      require([
        "dojo/ready",
        "dojo/parser", 
        "dojo/_base/array",
        "dojo/dom", 
        "dojo/dom-prop",

        "esri/map",
        "esri/layers/ArcGISDynamicMapServiceLayer",
        "esri/layers/ArcGISTiledMapServiceLayer",

        "dijit/layout/BorderContainer", 
        "dijit/layout/ContentPane"
      ], function(
        ready, parser, array, dom, domProp,
        Map, ArcGISDynamicMapServiceLayer, ArcGISTiledMapServiceLayer
      ) {
        ready(function() {
          // parse layout dijits
          parser.parse();

          var map = new Map("mapDiv");
          var basemap = new ArcGISTiledMapServiceLayer("https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/TaxParcel/AssessorsBasemap/MapServer");
          map.addLayer(basemap);

          var census = new ArcGISDynamicMapServiceLayer("https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer", {
            "id": "census",
            "opacity": 0.5
          });
          map.addLayer(census);

          map.on("load", function(evt) {
            //display layer details in the bottom content pane 
            var layerInfo = [], map = evt.map;

            array.forEach(map.layerIds, function(id) {
              var layer = map.getLayer(id);
              layerInfo.push("id: " + layer.id + " visible: " + layer.visible + " opacity: " + layer.opacity + "<br />");
            });

            var sr = "Spatial Reference: " + map.spatialReference.wkid + "<br />";
            var size = "Width: " + map.width + " Height: " + map.height + "<br />";

            //get scale detail
            var tileInfo = basemap.tileInfo;
            var scales = [];
            array.forEach(tileInfo.lods, function(lod) {
              var level = lod.level;
              var scale = lod.scale;
              var resolution = lod.resolution;
              scales.push("level: " + level + " scale: " + scale + " resolution: " + resolution + "<br />");
            });
            domProp.set("mapInfo", "innerHTML", "<b>Layers:</b><br />" + layerInfo.join("") +
                    "<b>Spatial Reference:</b> <br /> " + sr +
                    "<b>Map Size:</b> <br />" + size +
                    "<b>Scales:</b><br />" + scales.join("")
            );
          });

          //Update the extent info in the layer details panel when the map's extent changes 
          map.on("extent-change", function(evt) {
            var s = "<b>Extent</b><br />",
                extent = evt.extent;
            s += "XMin: " + extent.xmin.toFixed(4) + " " + "YMin: " + extent.ymin.toFixed(4) + " " + "XMax: " + extent.xmax.toFixed(4) + " " + "YMax: " + extent.ymax.toFixed(4);
            domProp.set(dom.byId("currentInfo"), "innerHTML", s);
          });
        });
      });
    </script>
  </head>
  <body class="tundra">
    <div id="content" 
         data-dojo-type="dijit/layout/BorderContainer" 
         data-dojo-props="design:'headline', gutters:true" 
         style="width: 100%; height: 100%; margin: 0;"> 
      
      <div id="mapDiv" 
           data-dojo-type="dijit/layout/ContentPane" 
           data-dojo-props="region:'center'" 
           style="overflow:hidden;"> 
      </div> 

      <div id="bottomPane" 
           style="height:200px;" 
           data-dojo-type="dijit/layout/ContentPane" 
           data-dojo-props="region:'bottom'">

        <div id="currentInfo"></div>
        <div id="mapInfo"></div>

      </div>

    </div> 
  </body>
</html>
 
          
Show Modal