Select to view content in your preferred language

Weird Problem Loading Layers From Array

627
1
10-19-2010 12:45 PM
ZacharyNewell
New Contributor
I am having problems using the array below for loading layers into a map.
        
var layers= [
     { "id": "geothermal",
         "url": "http://gisweb.unr.edu/ArcGIS/rest/services/Geologic/MapServer",
         "label": "Great Basin Geothermal Data",
         "type": "dynamic",
         "queryLayers": [8],
         "visible": true
     },
 { "id": "geothermalExplore",
      "url": "http://gisweb.unr.edu/ArcGIS/rest/services/Explore/MapServer",
      "label": "Great Basin Exploration Data",
      "type": "dynamic",
      "queryLayers": [6],
      "visible": false
  },
 { "id": "geothermalFav",
      "url": "http://gisweb.unr.edu/ArcGIS/rest/services/FavLayers/MapServer",
      "label": "Great Basin Favorability Data",
      "type": "dynamic",
      "queryLayers": [6],
      "visible": false
  },
 { "id": "geothermalPhy",
      "url": "http://gisweb.unr.edu/ArcGIS/rest/services/Geophysall/MapServer",
      "label": "Great Basin Geophysical Data",
      "type": "dynamic",
      "queryLayers": [6],
      "visible": false
  }
  
   ];


Now depending on the order of list determines if there will be an error(a layer not being added). I've isolated it to this service:
{ "id": "geothermalPhy",
 "url": "http://gisweb.unr.edu/ArcGIS/rest/services/Geophysall/MapServer",
 "label": "Great Basin Geophysical Data",
  "type": "dynamic",
  "queryLayers": [6],
  "visible": false
}


If it is placed in the array before any other object it causes the others to error out(the message is that the object is undefined). It only happens in IE. Here is the function that adds the layers.
        function AddAllLayers() {
            for (var i = 0, il = layers.length; i < il; i++) {
    
                var info = geothermalLayers;
                //console.log(info.url)
                var layer;

                //Determine if the map is dynamic or tiled
                if (info.type == 'dynamic') {
                    layer = new esri.layers.ArcGISDynamicMapServiceLayer(info.url, {
                        id: info.id,
                        visible: info.visible
                    });
                }
                else {
                    layer = new esri.layers.ArcGISTiledMapServiceLayer(info.url, {
                        id: info.id,
                        visible: info.visible
                    });
                }
    
                //console.log(layer);
                layer.label = info.label;

  //Only doing one queryLayer now
                layer.queryLayer = info.url + "\\" + info.queryLayers[0];

  dojo.connect(layer,"onError",function(error){
    console.log(error.message);
   });

                map.addLayer(layer);

        }
}


Any insight would be appreciated.
0 Kudos
1 Reply
TimRourke
Emerging Contributor
I've run into this with layers that don't load right away. So you may have a layer that isn't completely loaded before you add it to the map. Here's the solution I've found for that:

    function addDynamicLayer(mapServiceUrl, layerId, isVisible)
    {
        var layer = new esri.layers.ArcGISDynamicMapServiceLayer(mapServiceUrl, { id: layerId, visible: isVisible });
        if (layer.loaded)
        {
            this.addLayerToMap.call(this, layer);
        }
        else
        {
            dojo.connect(layer, "onLoad", this, "addLayerToMap");
        }
        return layer;
    }
    
     function addLayerToMap(layer)
    {
        this.Map.addLayer(layer);
    }
Hope that applies to your problem...
0 Kudos