Select to view content in your preferred language

Get list of layers from map service - v3.6 API

3080
1
Jump to solution
08-26-2013 12:09 PM
GISAdmin1
Regular Contributor
I am having trouble getting a list of layers from a map service automatically.  If I assign the get layer list function to a button, I have no problem.  Here is
require(["esri/map", "esri/geometry/Extent", "esri/layers/ArcGISTiledMapServiceLayer", "esri/layers/ArcGISDynamicMapServiceLayer" ,"dojo/domReady!"], function(Map, Extent, ArcGISTiledMapServiceLayer,ArcGISDynamicMapServiceLayer,LayerInfo) {  var map = new Map("map", {extent: new Extent({"xmin":506835.3785030904,"ymin":5439759.165184998,"xmax":515698.93789687584,"ymax":5450607.1035475405,"spatialReference":{"wkid":26910}})  });  var baseMap = new ArcGISTiledMapServiceLayer("http://cosmos.surrey.ca/COSREST/rest/services/Base_Map_All_Scales/MapServer"); allCityHighUse = new ArcGISDynamicMapServiceLayer("http://cosmosbeta.surrey.ca/COSREST/rest/services/Public/MapServer"); map.addLayer(baseMap, allCityHighUse);  });   alert(getlayerID ('Test Layer'));  function getlayerId(layerName) {               var layerInfos =  allCityHighUse.layerInfos;                 for (var i=0, il=layerInfos.length; i<il; i++) {             var templayerName = layerInfos.name;             var layerId = layerInfos.id;                            if (templayerName == layerName) {                 alert(layerId);               }             return layerId;          }    }  


Any idea why?  If I console.log (allCityHighUse), during runtime I see the layerInfos object is populated.
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
What's happening is the alert fires before the layer is ready. You should run that function in the layer's load event or the map's layers-add-result event.
require(["esri/map", "esri/geometry/Extent", "esri/layers/ArcGISTiledMapServiceLayer", "esri/layers/ArcGISDynamicMapServiceLayer" ,"dojo/domReady!"], function(Map, Extent, ArcGISTiledMapServiceLayer,ArcGISDynamicMapServiceLayer,LayerInfo) {     var map = new Map("map", {extent: new Extent({"xmin":506835.3785030904,"ymin":5439759.165184998,"xmax":515698.93789687584,"ymax":5450607.1035475405,"spatialReference":{"wkid":26910}})  });     var baseMap = new ArcGISTiledMapServiceLayer("http://cosmos.surrey.ca/COSREST/rest/services/Base_Map_All_Scales/MapServer");     allCityHighUse = new ArcGISDynamicMapServiceLayer("http://cosmosbeta.surrey.ca/COSREST/rest/services/Public/MapServer");     map.addLayer(baseMap, allCityHighUse);      allCityHighUse.on("load" function () {         alert(getlayerID ('Test Layer'));     });      //or     //map.on("layers-add-result", function() {     //    alert(getlayerID ('Test Layer'));     //});    });      //alert(getlayerID ('Test Layer'));  function getlayerId(layerName) {                         var layerInfos =  allCityHighUse.layerInfos;                    for (var i=0, il=layerInfos.length; i<il; i++) {         var templayerName = layerInfos.name;         var layerId = layerInfos.id;                                      if (templayerName == layerName) {             alert(layerId);         }         return layerId;     } }

View solution in original post

0 Kudos
1 Reply
KenBuja
MVP Esteemed Contributor
What's happening is the alert fires before the layer is ready. You should run that function in the layer's load event or the map's layers-add-result event.
require(["esri/map", "esri/geometry/Extent", "esri/layers/ArcGISTiledMapServiceLayer", "esri/layers/ArcGISDynamicMapServiceLayer" ,"dojo/domReady!"], function(Map, Extent, ArcGISTiledMapServiceLayer,ArcGISDynamicMapServiceLayer,LayerInfo) {     var map = new Map("map", {extent: new Extent({"xmin":506835.3785030904,"ymin":5439759.165184998,"xmax":515698.93789687584,"ymax":5450607.1035475405,"spatialReference":{"wkid":26910}})  });     var baseMap = new ArcGISTiledMapServiceLayer("http://cosmos.surrey.ca/COSREST/rest/services/Base_Map_All_Scales/MapServer");     allCityHighUse = new ArcGISDynamicMapServiceLayer("http://cosmosbeta.surrey.ca/COSREST/rest/services/Public/MapServer");     map.addLayer(baseMap, allCityHighUse);      allCityHighUse.on("load" function () {         alert(getlayerID ('Test Layer'));     });      //or     //map.on("layers-add-result", function() {     //    alert(getlayerID ('Test Layer'));     //});    });      //alert(getlayerID ('Test Layer'));  function getlayerId(layerName) {                         var layerInfos =  allCityHighUse.layerInfos;                    for (var i=0, il=layerInfos.length; i<il; i++) {         var templayerName = layerInfos.name;         var layerId = layerInfos.id;                                      if (templayerName == layerName) {             alert(layerId);         }         return layerId;     } }
0 Kudos