0

Can i create 2 legend objects in one script?

I'm currently working on a web-application in which i already do have a legend/TOC running and it looks like this:

var legendLayers=[];
    legendLayers.push({layer:raum, title:'Raumkategorien', slider:true});
        map.on("layers-add-result", function(){
        var layerInfo = new TOC({
                map: map,
                layerInfos: legendLayers,
        }, "legendDiv");
        layerInfo.startup();
    });
    map.addLayers([raum]);

Now I added some geoprocessing functionality and i wanted to add a new TOC when the result map is loaded in the web-app. I tried this by doing the following:

function gpJobComplete(jobinfo){
        var mapurl = mapServiceURL + "/" + jobinfo.jobId;
        var energy = new ArcGISDynamicMapServiceLayer(mapurl,{
            id:"Beste Energieformen",
            "opacity": 0.85
        });
        map.addLayers([energy]);

        var outputLayers = [];
        outputLayers.push({layer:energy, title:"Beste Energieformen"});
        map.on("layers-add-result", function(){
            domUtils.show(dom.byId('outputDiv'));
            var outputInfo = new TOC({
                map: map,
                layerInfos: outputLayers
            }, "outputDiv");
        outputInfo.startup();
        });
    }

I get a result map and it works just nice in the web app, but there's no new TOC. Am i trying it the wrong way?

1

Have you destroyed your previous TOC? I've found that the legend widget get's confused if you build a new one without first destroying the old one. The legend widget has a destroy method, I'm not sure if the TOC does as well. Try completely removing the old TOC before building a new one.

Good Luck.

It's essentially something like this. But as I said I'm just using the legend widget not the TOC widget.

        if (biosatApp.hasOwnProperty("legend")) {
            biosatApp.legend.destroy();
            domConstruct.destroy(dojo.byId("legendDiv"));
        }
        // create a new div for the legend
        var legendDiv = domConstruct.create("div", {
            id: "legendDiv"
        }, dom.byId("EILegendContainer"));

        biosatApp.legend = new Legend({
            map: biosatApp.map,
            layerInfos: biosatApp.legendLayers
        }, legendDiv);
        biosatApp.legend.startup();
  • @Simmal try destroying the TOC. I think layerInfos is the list of legend layers, but not the actual legend. – NBabel Mar 31 '14 at 21:42
  • No, i destroyed layerInfo not layerInfos and layerInfo was the TOC (var layerInfo = new TOC). See the first script. I saw that you had some similar problem. Is it possible that i can have a look on your script and how it worked? – Simmal Apr 1 '14 at 15:16
  • I just realised that the TOC won't show after running the geoprocessing task even though i uncommented the first (existing) TOC. – Simmal Apr 1 '14 at 22:31
  • If you did a destroy it may have obliterated the entire div that it was in. You have to recreate the div to create a new legend/TOC in it. If you notice in the code above I have to create a div for the legend after destroying it. – NBabel Apr 2 '14 at 19:14
  • Thanks. That obviously worked. I just had the problem that i defined my variable for the existing TOC in another funstion. I think that might have been the problem so that the second function didn't know about that variable. – Simmal Apr 4 '14 at 14:55
0

so i tried destroying the existing toc and creating a new one like this, but it doesn't seem to work

function gpJobComplete(jobinfo){
        var mapurl = mapServiceURL + "/" + jobinfo.jobId;
        var energy = new ArcGISDynamicMapServiceLayer(mapurl,{
            id:"Beste Energieformen",
            "opacity": 0.85
        });

        layerInfo.destroy();
        var outputLayers = [];
        outputLayers.push({layer:energy, title:"Beste Energieformen"});
        map.on("layers-add-result", function(){
            var outputInfo = new TOC({
                map: map,
                layerInfos: outputLayers
            }, "outputDiv");
        outputInfo.startup();
        });
        map.addLayers([energy]);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.