Select to view content in your preferred language

Get Layer Name

1455
2
Jump to solution
12-12-2012 05:18 AM
ChrisSergent
Regular Contributor III
I am attempting to display the name of a layer in an alert box. I have attached the .NET project. Here is the JavaScript code that I am using with a comment above the line where I am attempting to get the name of the layer. Any ideas what I need to do? :
<script type="text/javascript">
        dojo.require("esri.map");
        dojo.require("esri.layers.agstiled"); // cached map service.
       

        var map;

        function init() {
            map = new esri.Map("mapDiv");
           
            dojo.connect(window, 'resize', map, map.resize);

            var baseMapURL = "http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer";
            var basemap = new esri.layers.ArcGISTiledMapServiceLayer(baseMapURL);

            map.addLayer(basemap);
            // I want this alert to display the layer name which should be Topographic Info.
            alert(basemap.layerInfos[0].name);       
           
        }
        dojo.addOnLoad(init);
       
       
    </script>


Thanks.

Chris S.
0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Frequent Contributor II
You'll want to listen to the 'onLayerAdd' event when adding individual layers.
http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jsapi/map.htm#onLayerAdd

So something like
dojo.connect(map, 'onLayerAdd', function(){   alert(basemap.layerInfos[0].name); });  map.addLayer(basemap);


Right now you're trying to access the layerInfos immediately after adding it to the map, but you have to wait for it to finish loading before you can interact with it.

You might even be able to listen to the individual layers onLoad, but I haven't tried that way.
http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jsapi/layer.htm#onLoad

View solution in original post

2 Replies
ReneRubalcava
Frequent Contributor II
You'll want to listen to the 'onLayerAdd' event when adding individual layers.
http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jsapi/map.htm#onLayerAdd

So something like
dojo.connect(map, 'onLayerAdd', function(){   alert(basemap.layerInfos[0].name); });  map.addLayer(basemap);


Right now you're trying to access the layerInfos immediately after adding it to the map, but you have to wait for it to finish loading before you can interact with it.

You might even be able to listen to the individual layers onLoad, but I haven't tried that way.
http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jsapi/layer.htm#onLoad
ChrisSergent
Regular Contributor III
This worked perfect. Thanks Rene.

You'll want to listen to the 'onLayerAdd' event when adding individual layers.
http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jsapi/map.htm#onLayerAdd

So something like
dojo.connect(map, 'onLayerAdd', function(){
  alert(basemap.layerInfos[0].name);
});

map.addLayer(basemap);


Right now you're trying to access the layerInfos immediately after adding it to the map, but you have to wait for it to finish loading before you can interact with it.

You might even be able to listen to the individual layers onLoad, but I haven't tried that way.
http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jsapi/layer.htm#onLoad
0 Kudos