Select to view content in your preferred language

onLayersAddResult event not firing for multiple maps with the same layer

2601
5
08-02-2011 07:31 AM
NicolaAdamchik
Emerging Contributor
I have two maps on the same page that load the same layer.  The onLayersAddResult event only fires after one map loads the map layer and never fires for the other map unless I remove the duplicate layer.  The event correctly fires twice when the two maps have unique layers.

FYI - The layers load fine, it's just the event handling that is not working.
0 Kudos
5 Replies
derekswingley1
Deactivated User
Can you post some code to reproduce this?
0 Kudos
NicolaAdamchik
Emerging Contributor
var map = new esri.Map(mapPaneId, {
        logo: false
    });

// more code ...

if (config && config.dynamicLayerServices && config.dynamicLayerServices[0]) {
        hasDynamicServices = true;
        var layerCount = config.dynamicLayerServices.length;
        var layers = new Array();
        var layerInfos = new Array();
  
        for (var i = 0; i < layerCount; i++) {
            var layer = new esri.layers.ArcGISDynamicMapServiceLayer(config.dynamicLayerServices.url);
            var layerName = config.dynamicLayerServices.name;
            if (config.dynamicLayerServices.opacity) {
                layer.setOpacity(config.dynamicLayerServices.opacity);
            }
            layers.push(layer);
            layerInfos.push({ layer: layer, title: config.dynamicLayerServices.name, id: config.dynamicLayerServices.id });
            dojo.connect(layer, "onUpdate", hideLoadingIndicator);
            dojo.connect(layer, "onError", function(err) {
                console.log("Error retrieving the layer '" + layerName + "': " + err.message);
                //alert("Error retrieving the layer '" + layerName + "': " + err.message);
            });
        }

        dojo.connect(map, 'onLayersAddResult', function(results) {
            
           // THIS IS NOT FIRING WHEN 2 MAPS HAVE THE SAME LAYER
           //  --  EVEN IF THE LAYER ID IS DIFFERENT

            if (!config.hideToolbar) {
                initLayersDialog(layersDialog, map, layerInfos);
                initLegendDialog(legendDialog, map, layerInfos);
                initIdentifyTasks(div, map, config.dynamicLayerServices);
            }
            connectGridsToMap(div, map);
        });

        map.addLayers(layers);
0 Kudos
derekswingley1
Deactivated User
Are you adding the same layer object to two different maps?
0 Kudos
NicolaAdamchik
Emerging Contributor
The code that generates the layer and layerInfo objects is in a function that gets called separately for each map.

If I log the layer id (from the for loop), it prints out a unique string for each layer on the page.
0 Kudos
derekswingley1
Deactivated User
I put together a simple example with two maps and an event listener for onLayersAddResult for each:  http://jsfiddle.net/swingley/JSqnG/

Open the console, load that page and you'll see that both events fire. I'm sure your code has more going on but this is possible...if you want to post a more complete sample of what you're doing we might be able to help.
0 Kudos