AnsweredAssumed Answered

Load Layers from Config file

Question asked by LuciH on Sep 11, 2012
Latest reply on Sep 18, 2012 by LuciH
Is anyone familiar with loading layers from a config file?  I am trying to do it and I get various results depending upon the method I try.  The method quoted gives me duplicate entries of each layer added.  I have created the array called layrnames, but cannot determine proper function to use to load all the layers names I have pushed into the array.  Any help would be greatly appreciated :)

Thanks,  Luci

function init(){
                esri.config.defaults.geometryService = new esri.tasks.GeometryService("http://fwb-gissrv/ArcGIS/rest/services/Geometry/GeometryServer");
               
                //create popup for multiple popups
                var popup = new esri.dijit.Popup({
                    fillSymbol: new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 0, 0]), 2), new dojo.Color([240, 128, 128, 0.25]))
                }, dojo.create("div"));
               
                //map.spatialReference = new esri.SpatialReference({wkid:102100});
               
                //set initial extent
                var initExtent = new esri.geometry.Extent(-9651200, 3555500, -9636400, 3562470, new esri.SpatialReference({
                    "wkid": 102100
                }));
               
                locator = new esri.tasks.Locator("http://fwb-gissrv/ArcGIS/rest/services/GeoCodes/AddressessFWB/GeocodeServer");
                dojo.connect(locator, "onAddressToLocationsComplete", showResultsl);
               
                //load map
                map = new esri.Map("map", {
                    extent: initExtent,
                    slider: true,
                    lods: lods,
                    infoWindow: popup
                });
               
                //add basemap gallery tiles
                createBasemapGallery();
               
                dojo.connect(basemapGallery, "onError", function(msg){
                    console.log(msg)
                });
               
                dojo.xhrGet({
                    url: "Config.json",
                    handleAs: "json",
                    preventCache: true,
                    load: function(responseObject, ioArgs){
                        layrname = responseObject.Lyrname;
                       
                        //add layers from config file
                        if (layrname.length != 0) {
                            for (var i = 0; i < layrname.length; i++) {
                                layname = (layrname[i].lname);
                                laytitle = (layrname[i].ltitle);
                                layurl = (layrname[i].lUrl);
                                layrvis = (layrname[i].lVisb);
                                layid = (layrname[i].lyid);
                                layname = new esri.layers.ArcGISDynamicMapServiceLayer(layurl, {
                                    id: layid,
                                    visible: layrvis
                                });
                                legendLayers.push({
                                    layer: layname,
                                    title: laytitle
                                });
        layrnames.push({
         layern: layname
        });
        map.addLayer(layname);
                            }
                        };
     
                        dojo.connect(map, 'onLayeraddResult', function(results){
                            var legend = new esri.dijit.Legend({
                                map: map,
                                layerInfos: legendLayers
                            }, "legendDiv");
                            legend.startup();
                        });
                       
      //map.addLayers([layname]);
                                         
                        dojo.connect(map, 'onLayerAddResult', function(results){
                       
                            //add check boxes
                            dojo.forEach(legendLayers, function(layer){
                                var layerName = layer.title;
                                var checkBox = new dijit.form.CheckBox({
                                    name: "checkBox" + layer.layer.id,
                                    value: layer.layer.id,
                                    checked: layer.layer.visible,
                                    onChange: function(evt){
                                        var clayer = map.getLayer(this.value);
                                        clayer.setVisibility(!clayer.visible);
                                        this.checked = clayer.visible;
          legend.refresh();
                                    }
                                });
                               
                                //add the check box and label to the toc
                                dojo.place(checkBox.domNode, dojo.byId("toggle"), "after");
                                var checkLabel = dojo.create('label', {
                                    'for': checkBox.name,
                                    innerHTML: layerName
                                }, checkBox.domNode, "after");
                                dojo.place("<br />", checkLabel, "after");
                            });
                           
                        });
                       
                    }
                });

Outcomes