If I have been passed an esri.Map in a JavaScript function, how can I use the map object to get at a specific layer by name? The Map in question contains a map service, and the map service contains a number of sublayers (one of which I need). In my case, I want to get the MinScale and MaxScale for layer "0" (named "KMPosts" of this map service: River/GDT_SDE (MapServer)
Is it possible to do that without making a round-trip to the server?
Solved! Go to Solution.
River Taig,
Sure just use the map.getLayer("your dynamic layers ID"). If you do not know the layers exact id or the id was not specified when the layer was added so it got an map generated id then you can use the map.layerIds array and loop though those till you find your layerId. The you would get the layerInfos array and loop thought that to get the sublayer names and minScale and maxScale.
Here is a sample to show this in action:
<!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.16/esri/css/esri.css"/> <style> html, body, #mapDiv { padding: 0; margin: 0; height: 100%; } </style> <script src="https://js.arcgis.com/3.16/"></script> <script> var map, dynamicMapServiceLayer; require([ "esri/map", "esri/layers/ArcGISDynamicMapServiceLayer", "dojo/on", "dojo/_base/lang", "dojo/_base/array", "dojo/domReady!" ], function (Map, ArcGISDynamicMapServiceLayer, on, lang, array) { map = new Map("mapDiv", { basemap: "streets", center: [-98.258, 38.236], zoom: 7 }); dynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer("https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer"); map.addLayers([dynamicMapServiceLayer]); on(map, "layers-add-result", function(){ getLayerInfoFromMap(map, dynamicMapServiceLayer.id, "Detailed Counties"); }); function getLayerInfoFromMap (themap, dlid, subLyrName) { var dLyr = themap.getLayer(dlid); var minscale, maxscale; array.some(dLyr.layerInfos, function(lyrInfo){ if(lyrInfo.name === subLyrName){ minscale = lyrInfo.minScale; maxscale = lyrInfo.maxScale; return true; } }, this); console.info(minscale, maxscale); } }); </script> </head> <body> <div id="mapDiv"></div> </body> </html>
River Taig,
Sure just use the map.getLayer("your dynamic layers ID"). If you do not know the layers exact id or the id was not specified when the layer was added so it got an map generated id then you can use the map.layerIds array and loop though those till you find your layerId. The you would get the layerInfos array and loop thought that to get the sublayer names and minScale and maxScale.
Here is a sample to show this in action:
<!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.16/esri/css/esri.css"/> <style> html, body, #mapDiv { padding: 0; margin: 0; height: 100%; } </style> <script src="https://js.arcgis.com/3.16/"></script> <script> var map, dynamicMapServiceLayer; require([ "esri/map", "esri/layers/ArcGISDynamicMapServiceLayer", "dojo/on", "dojo/_base/lang", "dojo/_base/array", "dojo/domReady!" ], function (Map, ArcGISDynamicMapServiceLayer, on, lang, array) { map = new Map("mapDiv", { basemap: "streets", center: [-98.258, 38.236], zoom: 7 }); dynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer("https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer"); map.addLayers([dynamicMapServiceLayer]); on(map, "layers-add-result", function(){ getLayerInfoFromMap(map, dynamicMapServiceLayer.id, "Detailed Counties"); }); function getLayerInfoFromMap (themap, dlid, subLyrName) { var dLyr = themap.getLayer(dlid); var minscale, maxscale; array.some(dLyr.layerInfos, function(lyrInfo){ if(lyrInfo.name === subLyrName){ minscale = lyrInfo.minScale; maxscale = lyrInfo.maxScale; return true; } }, this); console.info(minscale, maxscale); } }); </script> </head> <body> <div id="mapDiv"></div> </body> </html>
Oh fantastic - That'll do it. I had seen LayerInfo, but have looked past it before because it didn't have methods on it like queryFeatures that I needed (which are on the FeatureLayer). Ideally there would be some way to "hydrate" a FeatureLayer from a LayerInfo